android PowerManager(电源管理) wakelock(屏幕锁)
2011-07-21 18:31
375 查看
今天在看一个项目的源代码时,发现了这个电源管理和屏幕锁:PowerManager、wakelock。
其中在onStart()方法中获取wakelock,并且在onstop()中将这个lock释放掉。
protected void onStart() {
....
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "MediaCenterApplication");
wl.acquire();
wl.setReferenceCounted(false);
....
}
protected void onStop() {
....
wl.release();
....
}
PowerManager和WakeLock的操作步骤 PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);通过
然后通过PowerManager的newWakeLock
((int flags, String
tag)来生成WakeLock实例。int Flags指示要获取哪种WakeLock,不同的Lock对cpu 、屏幕、键盘灯有不同影响。
获取WakeLock实例后通过acquire()获取相应的锁,然后进行其他业务逻辑的操作,最后使用release()释放(释放是必须的)。
其中在onStart()方法中获取wakelock,并且在onstop()中将这个lock释放掉。
protected void onStart() {
....
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "MediaCenterApplication");
wl.acquire();
wl.setReferenceCounted(false);
....
}
protected void onStop() {
....
wl.release();
....
}
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-permission android:name="android.permission.WAKE_LOCK"/> 你可能还需要 <uses-permission android:name="android.permission.DEVICE_POWER"/>
相关文章推荐
- Android中如何让手机屏幕不待机(PowerManager 和WakeLock)
- android之PowerManager 与电源管理,解决灭屏状态下来短信屏幕不会点亮问题
- Android WakeLock 使屏幕保持亮度,不要锁屏
- android中涉及wi-fi管理,internetManager网络管理,connectivityManager连接管理,PowerManager电源管理,vibrator震动管理等参数问题
- Android之PowerManager电源管理
- android通过电源管理保持屏幕常亮
- android 电源管理,屏幕锁 ,小米2s点亮屏幕,解锁
- Android系统中的服务管理类(ActivityManager、PowerManager(电源管理)、AudioManager(音频管理)。PackageManager(管理应用程序包))
- android 电源管理,屏幕锁 ,小米2s点亮屏幕,解锁
- Android之PowerManager电源管理
- android 电源管理 wakelock 唤醒锁机制
- Android之PowerManager电源管理
- android powermanager 电源管理
- android 之 PowerManager 与电源管理
- android 之 PowerManager 与电源管理
- android 之 PowerManager 与电源管理
- Android入门进阶教程(19)-PowerManager 与电源管理
- android 之 PowerManager 与电源管理
- Android开发:屏幕常亮 PowerManager和PowerManager.WakeLock
- android 电源管理(PowerManager)