English | Site Directory

Android - An Open Handset Alliance Project

android.os
public class

android.os.PowerManager

java.lang.Object
android.os.PowerManager

Class that lets you control the power state of the device. Get an instance of this class by calling Context.getSystemService().

Nested Classes
PowerManager.WakeLock Class lets you say that you need to have the device on. 

Summary

Constants

      Value  
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.  268435456  0x10000000 
int  FULL_WAKE_LOCK  Wake lock that ensures that the screen and keyboard are on at full brightness.  0x00000002 
int  ON_AFTER_RELEASE  When this wake lock is released, poke the user activity timer so the screen stays on for a little longer.  536870912  0x20000000 
int  PARTIAL_WAKE_LOCK  Wake lock that ensures that the CPU is running.  0x00000001 
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.  0x00000003 
int  SCREEN_DIM_WAKE_LOCK  Wake lock that ensures that the screen is on, but the keyboard backlight will be allowed to go off, and the screen backlight will be allowed to go dim.  0x00000004 

Public Methods

        void  goToSleep(long time)
Force the device to go to sleep.
        WakeLock  newWakeLock(int flags, String tag)
Get a wake lock at the level of the flags parameter.
        void  userActivity(long when, boolean noChangeLights)
User activity happened.
Methods inherited from class java.lang.Object

Details

Constants

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.

Constant Value: 268435456 (0x10000000)

public static final int FULL_WAKE_LOCK

Wake lock that ensures that the screen and keyboard are on at full brightness.
Constant Value: 2 (0x00000002)

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.

Does not work with PARTIAL_WAKE_LOCKs.

Constant Value: 536870912 (0x20000000)

public static final int PARTIAL_WAKE_LOCK

Wake lock that ensures that the CPU is running. The screen might not be on.
Constant Value: 1 (0x00000001)

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.
Constant Value: 3 (0x00000003)

public static final int SCREEN_DIM_WAKE_LOCK

Wake lock that ensures that the screen is on, but the keyboard backlight will be allowed to go off, and the screen backlight will be allowed to go dim.
Constant Value: 4 (0x00000004)

Public Methods

public void goToSleep(long time)

Force the device to go to sleep. Overrides all the wake locks that are held.

Parameters

time is used to order this correctly with the wake lock calls. The time should be in the SystemClock.uptimeMillis() time base.

public WakeLock newWakeLock(int flags, String tag)

Get a wake lock at the level of the flags parameter. Call acquire() on the object to acquire the wake lock, and release() when you are done.
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();
 

Parameters

flags PARTIAL_WAKE_LOCK or FULL_WAKE_LOCK
tag Your class name (or other tag) for debugging purposes.

public void userActivity(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.

Parameters

when is used to order this correctly with the wake lock calls. This time should be in the SystemClock.uptimeMillis() time base.
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.
Build m5-rc15g - 14 May 2008 12:50