What is WakeLock?


In order to prolong battery life, over time Android devices will first dim, then turn off the screen,
before turning off the CPU. WakeLocks are a Power Manager system Service feature, available to your
applications to control the power state of the host device.
Wake Locks can be used to keep the CPU running, prevent the screen from dimming, prevent the screen
from turning off, and prevent the keyboard backlight from turning off.

Creating and holding Wake Locks can have a dramatic influence on the battery
drain associated with your application. It’s good practice to use Wake Locks only
when strictly necessary, for as short a time as needed, and to release them as soon
as possible.


To create a Wake Lock, call newWakeLock on the Power Manager, specifying one of the following Wake
Lock types:
➤ FULL_WAKE_LOCK Keeps the screen at full brightness, the keyboard backlight illuminated,
   and the CPU running.
➤ SCREEN_BRIGHT_WAKE_LOCK
Keeps the screen at full brightness, and the CPU running.


➤ SCREEN_DIM_WAKE_LOCK
➤ PARTIAL_WAKE_LOCK

Keeps the screen on (but lets it dim) and the CPU running.
Keeps the CPU running.
PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyWakeLock");


PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyWakeLock");
wakeLock.acquire();
[ ... Do things requiring the CPU stay active ... ]
wakeLock.release();


Comments

Popular posts from this blog

Bluetooth Data Transfer Example

How to Create & Extract tar.gz and tar.bz2 Files in Linux