PowerManager.WakeLock
来源:互联网 发布:软件测试面试技巧 编辑:程序博客网 时间:2024/05/21 13:01
if(wakeLock==null) {
Logger.d("Acquiring wake lock");
PowerManager pm=(PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock=pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK,this.getClass().getCanonicalName());
wakeLock.acquire();
}
}
privatevoidreleaseWakeLock() {
if(wakeLock!=null&&wakeLock.isHeld()) {
wakeLock.release();
wakeLock=null;
}
}
acquireWakeLock()方法中获取了 SCREEN_DIM_WAKE_LOCK锁,该锁使 CPU 保持运转,屏幕保持亮度(可以变灰)。这个函数在Activity的 onResume中被调用。releaseWakeLock()方法则是释放该锁。它在Activity的 onPause中被调用。利用Activiy的生命周期,巧妙的让 acquire()和release()成对出现。@Override
protectedvoidonResume()
{
super.onResume();
//获取锁,保持屏幕亮度
acquireWakeLock();
startTimer();
}代码 protectedvoidonPause()
{
super.onPause();
synchronized(this) {
cancelTimer();
releaseWakeLock();
if(finished) {
clearState();
}else{
saveState();
}
}
}
PowerManager和WakeLock的操作步骤 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);通过 Context.getSystemService().方法获取PowerManager实例。 然后通过PowerManager的newWakeLock((int flags, String tag)来生成WakeLock实例。int Flags指示要获取哪种WakeLock,不同的Lock对cpu 、屏幕、键盘灯有不同影响。 获取WakeLock实例后通过acquire()获取相应的锁,然后进行其他业务逻辑的操作,最后使用release()释放(释放是必须的)。关于int flags 各种锁的类型对CPU 、屏幕、键盘的影响:
PARTIAL_WAKE_LOCK:保持CPU 运转,屏幕和键盘灯有可能是关闭的。
SCREEN_DIM_WAKE_LOCK:保持CPU 运转,允许保持屏幕显示但有可能是灰的,允许关闭键盘灯
SCREEN_BRIGHT_WAKE_LOCK:保持CPU 运转,允许保持屏幕高亮显示,允许关闭键盘灯
FULL_WAKE_LOCK:保持CPU 运转,保持屏幕高亮显示,键盘灯也保持亮度
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:f 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.
权限获取 要进行电源的操作需要在AndroidManifest.xml中声明该应用有设置电源管理的权限。<uses-permissionandroid:name="android.permission.WAKE_LOCK"/>你可能还需要
<uses-permissionandroid:name="android.permission.DEVICE_POWER"/>另外WakeLock的设置是 Activiy 级别的,不是针对整个Application应用的。
出处:http://hi.baidu.com/andsun_develop/item/a83840cd519aa03898b4989b
- PowerManager WakeLock
- PowerManager.WakeLock
- PowerManager.WakeLock
- PowerManager.WakeLock
- PowerManager.WakeLock
- PowerManager.WakeLock
- PowerManager和PowerManager.WakeLock
- PowerManager和PowerManager.WakeLock
- PowerManager和PowerManager.WakeLock详解
- Android: PowerManager.WakeLock
- android PowerManager wakelock
- Android PowerManager.WakeLock分享
- android PowerManager wakelock
- PowerManager.WakeLock电源管理
- PowerManager.WakeLock电源管理
- PowerManager.WakeLock的使用
- android PowerManager wakelock
- PowerManager.WakeLock源码解读
- Mysql监控工具–mytop
- .net中js方法,设置在文本框中光标位置处插入数据
- 可执行文件的装载
- UVA 465
- .net序列化及反序列化
- PowerManager.WakeLock
- Unity3D总结:关于射线碰撞
- PHP中常用函数简单介绍
- 写高效的JavaScript程序
- vs2005 的C++单元测试
- 【面向对象】如何在静态方法中使用非静态变量?
- 软件打包工具
- POJ 1068 Parencodings
- 如何安装中文的man包