作者:朱博
PowerManager顾名思义用来控制设备的电量。
使用这个API会对设备电池的寿命造成影响。除非真的需要,否则一般不推荐使用,即使你使用了该API也要尽可能使用
低级别并保证每次在申请完wakelock(唤醒锁自己翻译的………)应即时的释放它。
你可以通过调用Context.getSystemService().来获得本类实例。
本类的简单用法:
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 duringthis section..
wl.release();
Flag Value | CPU | 屏幕 | 键盘 |
PARTIAL_WAKE_LOCK | 开 | 关 | 关 |
SCREEN_DIM_WAKE_LOCK | 开 | 暗 | 关 |
SCREEN_BRIGHT_WAKE_LOCK | 开 | 亮 | 亮 |
FULL_WAKE_LOCK | 开 | 亮 | 亮 |
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. |
权限获取
< uses-permission android:name ="android.permission.WAKE_LOCK" /> 你可能还需要 < uses-permission android:name ="android.permission.DEVICE_POWER" />
有时间证实一下什么效果 嘿嘿