android-API之PowerManager 电源管理类

转载 2012年03月28日 14:16:43

public class

PowerManager

extends Object
java.lang.Object
   ↳ android.os.PowerManager

Class Overview

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.

用户使用这个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 Context.getSystemService().

The primary API you'll use is newWakeLock(). This will create a 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:

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
PARTIAL_WAKE_LOCK On* Off Off
SCREEN_DIM_WAKE_LOCK On Dim Off
SCREEN_BRIGHT_WAKE_LOCK On Bright Off
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 PARTIAL_WAKE_LOCK.

Flag Value Description
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.
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.

 

Summary

Nested Classes
class PowerManager.WakeLock Class lets you say that you need to have the device on. 
Constants
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.
int FULL_WAKE_LOCK Wake lock that ensures that the screen and keyboard are on at full brightness.
int ON_AFTER_RELEASE When this wake lock is released, poke the user activity timer so the screen stays on for a little longer.
int PARTIAL_WAKE_LOCK Wake lock that ensures that the CPU is running.
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.
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 Methods
void goToSleep(long time)
Force the device to go to sleep.
boolean isScreenOn()
Returns whether the screen is currently on.
PowerManager.WakeLock newWakeLock(int flags, String tag)
Get a wake lock at the level of the flags parameter.
void reboot(String reason)
Reboot the device.
void userActivity(long when, boolean noChangeLights)
User activity happened.
[Expand]
Inherited Methods
 From class java.lang.Object
Object clone()
Creates and returns a copy of this Object.
boolean equals(Object o)
Compares this instance with the specified object and indicates if they are equal.
void finalize()
Is called before the object's memory is being reclaimed by the VM.
finalClass<? extends Object> getClass()
Returns the unique instance of Class which represents this object's class.
int hashCode()
Returns an integer hash code for this object.
final void notify()
Causes a thread which is waiting on this object's monitor (by means of calling one of the wait() methods) to be woken up.
final void notifyAll()
Causes all threads which are waiting on this object's monitor (by means of calling one of the wait() methods) to be woken up.
String toString()
Returns a string containing a concise, human-readable description of this object.
final void wait(long millis, int nanos)
Causes the calling thread to wait until another thread calls the notify() ornotifyAll() method of this object or until the specified timeout expires.
final void wait(long millis)
Causes the calling thread to wait until another thread calls the notify() ornotifyAll() method of this object or until the specified timeout expires.
final void wait()
Causes the calling thread to wait until another thread calls the notify() ornotifyAll() method of this object.

Constants

public static final int ACQUIRE_CAUSES_WAKEUP

Since: API Level 1

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

Since: API Level 1

Wake lock that ensures that the screen and keyboard are on at full brightness.

Constant Value: 26 (0x0000001a)

public static final int ON_AFTER_RELEASE

Since: API Level 1

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 ACQUIRE_CAUSES_WAKEUP if you want that.

Does not work with PARTIAL_WAKE_LOCKs.

Constant Value: 536870912 (0x20000000)

public static final int PARTIAL_WAKE_LOCK

Since: API Level 1

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

Since: API Level 1

Wake lock that ensures that the screen is on at full brightness; the keyboard backlight will be allowed to go off.

Constant Value: 10 (0x0000000a)

public static final int SCREEN_DIM_WAKE_LOCK

Since: API Level 1

Wake lock that ensures that the screen is on (but may be dimmed); the keyboard backlight will be allowed to go off.

Constant Value: 6 (0x00000006)

Public Methods

public void goToSleep (long time)

Since: API Level 1

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 theSystemClock.uptimeMillis() time base.

public boolean isScreenOn ()

Since: API Level 7

Returns whether the screen is currently on. The screen could be bright or dim.

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); boolean isScreenOn = pm.isScreenOn();

 

Returns
  • whether the screen is on (bright or dim).

public PowerManager.WakeLock newWakeLock (int flags, String tag)

Since: API Level 1

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 Combination of flag values defining the requested behavior of the WakeLock.
tag Your class name (or other tag) for debugging purposes.

public void reboot (String reason)

Since: API Level 8

Reboot the device. Will not return if the reboot is successful. Requires the REBOOT permission.

Parameters
reason code to pass to the kernel (e.g., "recovery") to request special boot modes, or null.

public void userActivity (long when, boolean noChangeLights)

Since: API Level 1

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 theSystemClock.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. 

相关文章推荐

java 电源管理类 powermanager

  • 2013年11月21日 12:20
  • 47KB
  • 下载

电源管理类(PowerManager)以及唤醒屏幕锁的简单介绍

android.os.PowerManager 电源管理类 Device battery life will be significantly affected by the use of thi...
  • ggtaas
  • ggtaas
  • 2012年11月01日 21:59
  • 997

Android 电源管理PowerManager

PowerManager这个类提供了电源管理的一些功能,比如可以让屏幕或者键盘亮起来等。还有对设备的重启的api 官网是这么解释的 PowerManager Class Overview ...

Android之PowerManager电源管理

翻译来源PowerManager 这个类让你拥有控制设备状态的权利。 使用这个api类会很明显地影响设备电量的使用时长。除非你确实需要PowerManager.WakeLock否则不要轻易使用它们...

android之PowerManager 与电源管理,解决灭屏状态下来短信屏幕不会点亮问题

可解决灭屏状态下来短信屏幕不会点亮问题    PowerManager这个类主要是用来控制电源状态的. 通过使用该类提供的api可以控制电池的待机时间,一般情况下不要使用。如果确实需要使用,那...

Android之电源管理 PowerManager解决远程拍照手机黑屏问题

解决的问题: 这次使用PowerManager是因为我们后台远程拍照的时候,不支持手机黑屏情况拍照,如果手机黑屏,然后用PowerManager进行临时运行CPU 功能介绍: PowerMana...

Android入门进阶教程(19)-PowerManager 与电源管理

PowerManager这个类主要是用来控制电源状态的. 通过使用该类提供的api可以控制电池的待机时间,一般情况下不要使用。如果确实需要使用,那么尽可能的使用最低级别的WakeLocks锁。并且确保...

Android 4.0 中的PowerManager(电源管理--休眠唤醒)

转发请注明出处: http://blog.csdn.net/codectq/article/details/7324879         最近,在学习让系统进入休眠的过程中,学习了电源管理的...
  • codectq
  • codectq
  • 2012年03月06日 15:07
  • 13678

android 之 PowerManager 与电源管理

PowerManager这个类主要是用来控制电源状态的. 通过使用该类提供的api可以控制电池的待机时间,一般情况下不要使用。如果确实需要使用,那么尽可能的使用最低级别的WakeLocks锁。并且确保...
  • xieqibao
  • xieqibao
  • 2011年06月22日 22:48
  • 23429

CPU电源管理类文件打包

  • 2017年06月30日 14:18
  • 1.05MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:android-API之PowerManager 电源管理类
举报原因:
原因补充:

(最多只允许输入30个字)