Android开发之程序运行时保持屏幕、CPU、键盘灯的状态
Android程序運行時,可以使用WakeLock保證程序運行中玩家無操作時,設(shè)備的狀態(tài)。
PowerManager.WakeLock的使用:
Android使用Lock鎖對電源進行控制,lock鎖必須成對出現(xiàn)
請求鎖:
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 運轉(zhuǎn),屏幕和鍵盤燈有可能是關(guān)閉的。
SCREEN_DIM_WAKE_LOCK:保持CPU 運轉(zhuǎn),允許保持屏幕顯示但有可能是灰的,允許關(guān)閉鍵盤燈
SCREEN_BRIGHT_WAKE_LOCK:保持CPU 運轉(zhuǎn),允許保持屏幕高亮顯示,允許關(guān)閉鍵盤燈
FULL_WAKE_LOCK:保持CPU 運轉(zhuǎn),保持屏幕高亮顯示,鍵盤燈也保持亮度
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.
所需的權(quán)限:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
轉(zhuǎn)載于:https://www.cnblogs.com/Androider123/archive/2013/02/17/2914145.html
新人創(chuàng)作打卡挑戰(zhàn)賽發(fā)博客就能抽獎!定制產(chǎn)品紅包拿不停!總結(jié)
以上是生活随笔為你收集整理的Android开发之程序运行时保持屏幕、CPU、键盘灯的状态的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库的开发笔记-字典表
- 下一篇: 独立开发者之路