您的位置:首页 > 移动开发 > Android开发

Android开发之程序运行时保持屏幕、CPU、键盘灯的状态

2013-02-17 12:05 921 查看
Android程序运行时,可以使用WakeLock保证程序运行中玩家无操作时,设备的状态。

PowerManager.WakeLock的使用:
Android使用Lock锁对电源进行控制,lock锁必须成对出现

请求锁:
privatevoid acquireWakeLock() {
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();
}
}

释放锁:
privatevoid releaseWakeLock() {
if (wakeLock !=null&& wakeLock.isHeld()) {
wakeLock.release();
wakeLock =null;
}
}

在Activity的onResume中请求锁,onPause中释放锁

int flags:
获取WakeLock锁时传递的表示获取不同类型的锁,各种锁的类型对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.

所需的权限:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐