FileDocCategorySizeDatePackage
PowerManager.javaAPI DocAndroid 1.5 API13531Wed May 06 22:41:56 BST 2009android.os

PowerManager

public class PowerManager extends Object
This class gives you control of the power state of the device.

Device battery life will be significantly affected by the use of this API. Do not acquire WakeLocks unless you really need them, use the minimum levels possible, and be sure to release it as soon as you can.

You can obtain an instance of this class by calling {@link android.content.Context#getSystemService(java.lang.String) Context.getSystemService()}.

The primary API you'll use is {@link #newWakeLock(int, String) newWakeLock()}. This will create a {@link PowerManager.WakeLock} object. You can then use methods on this object to control the power state of the device. In practice it's quite simple: {@samplecode PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag"); wl.acquire(); ..screen will stay on during this section.. wl.release(); }

The following flags are defined, with varying effects on system power. These flags are mutually exclusive - you may only specify one of them.
Flag Value CPU Screen Keyboard
{@link #PARTIAL_WAKE_LOCK} On* Off Off
{@link #SCREEN_DIM_WAKE_LOCK} On Dim Off
{@link #SCREEN_BRIGHT_WAKE_LOCK} On Bright Off
{@link #FULL_WAKE_LOCK} On Bright Bright

*If you hold a partial wakelock, the CPU will continue to run, irrespective of any timers and even after the user presses the power button. In all other wakelocks, the CPU will run, but the user can still put the device to sleep using the power button.

In addition, you can add two more flags, which affect behavior of the screen only. These flags have no effect when combined with a {@link #PARTIAL_WAKE_LOCK}.
Flag Value Description
{@link #ACQUIRE_CAUSES_WAKEUP} Normal wake locks don't actually turn on the illumination. Instead, they cause the illumination to remain on once it turns on (e.g. from user activity). This flag will force the screen and/or keyboard to turn on immediately, when the WakeLock is acquired. A typical use would be for notifications which are important for the user to see immediately.
{@link #ON_AFTER_RELEASE} If this flag is set, the user activity timer will be reset when the WakeLock is released, causing the illumination to remain on a bit longer. This can be used to reduce flicker if you are cycling between wake lock conditions.

Fields Summary
private static final String
TAG
private static final int
WAKE_BIT_CPU_STRONG
These internal values define the underlying power elements that we might want to control individually. Eventually we'd like to expose them.
private static final int
WAKE_BIT_CPU_WEAK
private static final int
WAKE_BIT_SCREEN_DIM
private static final int
WAKE_BIT_SCREEN_BRIGHT
private static final int
WAKE_BIT_KEYBOARD_BRIGHT
private static final int
LOCK_MASK
public static final int
PARTIAL_WAKE_LOCK
Wake lock that ensures that the CPU is running. The screen might not be on.
public static final int
FULL_WAKE_LOCK
Wake lock that ensures that the screen and keyboard are on at full brightness.
public static final int
SCREEN_BRIGHT_WAKE_LOCK
Wake lock that ensures that the screen is on at full brightness; the keyboard backlight will be allowed to go off.
public static final int
SCREEN_DIM_WAKE_LOCK
Wake lock that ensures that the screen is on (but may be dimmed); the keyboard backlight will be allowed to go off.
public static final int
ACQUIRE_CAUSES_WAKEUP
Normally wake locks don't actually wake the device, they just cause it to remain on once it's already on. Think of the video player app as the normal behavior. Notifications that pop up and want the device to be on are the exception; use this flag to be like them.

Does not work with PARTIAL_WAKE_LOCKs.

public static final int
ON_AFTER_RELEASE
When this wake lock is released, poke the user activity timer so the screen stays on for a little longer.

Will not turn the screen on if it is not already on. See {@link #ACQUIRE_CAUSES_WAKEUP} if you want that.

Does not work with PARTIAL_WAKE_LOCKs.

IPowerManager
mService
TODO: It would be nice to be able to set the poke lock here, but I'm not sure what would be acceptable as an interface - either a PokeLock object (like WakeLock) or, possibly just a method call to set the poke lock.
Handler
mHandler
Constructors Summary
private PowerManager()

    
public PowerManager(IPowerManager service, Handler handler)
{@hide}

        mService = service;
        mHandler = handler;
    
Methods Summary
public voidgoToSleep(long time)
Force the device to go to sleep. Overrides all the wake locks that are held.

param
time is used to order this correctly with the wake lock calls. The time should be in the {@link SystemClock#uptimeMillis SystemClock.uptimeMillis()} time base.

        try {
            mService.goToSleep(time);
        } catch (RemoteException e) {
        }
    
public android.os.PowerManager$WakeLocknewWakeLock(int flags, java.lang.String tag)
Get a wake lock at the level of the flags parameter. Call {@link WakeLock#acquire() acquire()} on the object to acquire the wake lock, and {@link WakeLock#release release()} when you are done. {@samplecode PowerManager pm = (PowerManager)mContext.getSystemService( Context.POWER_SERVICE); PowerManager.WakeLock wl = pm.newWakeLock( PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, TAG); wl.acquire(); // ... wl.release(); }

param
flags Combination of flag values defining the requested behavior of the WakeLock.
param
tag Your class name (or other tag) for debugging purposes.
see
WakeLock#acquire()
see
WakeLock#release()

        return new WakeLock(flags, tag);
    
public voiduserActivity(long when, boolean noChangeLights)
User activity happened.

Turns the device from whatever state it's in to full on, and resets the auto-off timer.

param
when is used to order this correctly with the wake lock calls. This time should be in the {@link SystemClock#uptimeMillis SystemClock.uptimeMillis()} time base.
param
noChangeLights should be true if you don't want the lights to turn on because of this event. This is set when the power key goes down. We want the device to stay on while the button is down, but we're about to turn off. Otherwise the lights flash on and then off and it looks weird.

        try {
            mService.userActivity(when, noChangeLights);
        } catch (RemoteException e) {
        }