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"/>
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"/>
相关文章推荐
- JAVA Eclipse开发Android如何让屏幕保持为竖直或水平状态
- Android如何保持程序一直运行 (让屏幕常亮且停在当前程序界面)
- Android 锁屏状态下如何保持程序正常运行
- JAVA Eclipse开发Android如何让屏幕保持为竖直或水平状态
- Android开发运行程序自动进入调试状态<原创>
- 保持Android设备屏幕常亮和cpu活动状态
- Android开发之旅一监听程序的前后台运行状态
- react native开发过程中,运行android程序屏幕没有任何显示问题
- 重新想象 Windows 8 Store Apps (69) - 其它: 自定义启动屏幕, 程序的运行位置, 保持屏幕的点亮状态, MessageDialog, PopupMenu
- 重新想象 Windows 8 Store Apps (69) - 其它: 自定义启动屏幕, 程序的运行位置, 保持屏幕的点亮状态, MessageDialog, PopupMenu
- android app程序运行状态的判定
- Android开发中 如何保持登录状态
- Android学习小记-----监听并保存传感器数据,让service后台运行(保持CPU运转
- HTML5定稿了,终于有一种编程语言开发的程序可以在Android和IOS两种设备上运行了
- Eclipse开发Android程序如何在手机上运行
- Android 判断程序在手机中是否是活动状态或者正在运行状态
- Android开发教程--如何保持屏幕常亮
- Eclipse开发Android程序如何在手机上运行
- 解决Android开发中在调试模式下运行一个程序后出现Application (xxx) is waiting for the debugger to attach
- Android开发学习解决程序运行就出错的一个问题