PowerManager.WakeLock
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();
}
}
private void releaseWakeLock() {
if (wakeLock != null && wakeLock.isHeld()) {
wakeLock.release();
wakeLock = null;
}
}
acquireWakeLock()方法中獲取了 SCREEN_DIM_WAKE_LOCK鎖,該鎖使 CPU 保持運轉,屏幕保持亮度(可以變灰)。這個函數在Activity的 onResume中被調用。releaseWakeLock()方法則是釋放該鎖。它在Activity的 onPause中被調用。利用Activiy的生命週期,巧妙的讓 acquire()和release()成對出現。
protected void onResume()
{
super.onResume();
//獲取鎖,保持屏幕亮度
acquireWakeLock();
startTimer();
}
- PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);通過
Context.getSystemService()
.方法獲取PowerManager實例。 - 然後通過PowerManager的newWakeLock((intflags, String tag)來生成WakeLock實例。intFlags指示要獲取哪種WakeLock,不同的Lock對cpu 、屏幕、鍵盤燈有不同影響。
- 獲取WakeLock實例後通過acquire()獲取相應的鎖,然後進行其他業務邏輯的操作,最後使用release()釋放(釋放是必須的)。
關於int flags
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.DEVICE_POWER" />
*如果你獲得一個不完整的wakelock,則CPU會繼續運行,不必顧及任何計時器甚至用戶按下power鍵後。對於所有的其他的wakelock,CPU在運行,但用戶可以直接用power鍵使設備休眠。
*If you hold a partial wakelock, the CPU will continue to run, irrespective of any timers and even after the user presses the power button. In all other wakelocks, the CPU will run, but the user can still put the device to sleep using the power button.
另外,你能加兩個以上的標誌,這些僅能影響屏幕的行爲。這些標誌當組合中有一個PARTIAL_WAKE_LOCK時將沒有效果。
In addition, you can add
two more flags, which affect behavior of the screen only. These
flags have no effect when combined with a PARTIAL_WAKE_LOCK
.
Flag Value |
Description |
---|---|
|
正常的wake lock不會實際轉到照明。反而,他們使照明保持自他打開時(例如,來自用戶activity)。這個標誌會強制屏幕和/或鍵盤立即打開,當這個wakelock已獲取時。一個典型用法是將哪些重要的通知立即讓用戶看到。 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. |
|
如果這個標誌被設置,用戶activity計時器當WakeLock被釋放時會重新設定,因爲照明保持一點長。這個可以用於降低閃爍如果你是在wake lock狀態下循環。 If 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. |
Summary
Nested Classes |
|||
---|---|---|---|
class |
PowerManager.WakeLock |
類讓你說你需要有這個設備。 Class lets you say that you need to have the device on. |
Constants |
|||
---|---|---|---|
int |
ACQUIRE_CAUSES_WAKEUP |
正常的wake lock不會實際的喚醒設備,他們自他準備好則保持着。 Normally wake locks don't actually wake the device, they just cause it to remain on once it's already on. |
|
int |
FULL_WAKE_LOCK |
Wake lock 以確保屏幕和鍵盤全部點亮。 Wake lock that ensures that the screen and keyboard are on at full brightness. |
|
int |
ON_AFTER_RELEASE |
當這個wake lock已釋放時,撥開了用戶界面的計時器所以屏幕停留稍微長。 When this wake lock is released, poke the user activity timer so the screen stays on for a little longer. |
|
int |
PARTIAL_WAKE_LOCK |
Wake lock以確保CPU運行。 Wake lock that ensures that the CPU is running. |
|
int |
SCREEN_BRIGHT_WAKE_LOCK |
Wake lock以確保屏幕全亮;鍵盤背光允許關閉。 Wake lock that ensures that the screen is on at full brightness; the keyboard backlight will be allowed to go off. |
|
int |
SCREEN_DIM_WAKE_LOCK |
Wake lock以確保屏幕打開(但可以是暗淡的);鍵盤背光將允許關閉。 Wake lock that ensures that the screen is on (but may be dimmed); the keyboard backlight will be allowed to go off. |
Public Methods |
||
---|---|---|
void |
goToSleep(long time) 強制設備進入休眠。 Force the device to go to sleep. |
|
boolean |
IsScreenOn() 返回屏幕是否當前亮着。 Returns whether the screen is currently on. |
|
PowerManager.WakeLock |
newWakeLock(int flags, String tag) 得到一個Wake lock在這個標誌參數的級別。 Get a wake lock at the level of the flags parameter. |
|
void |
reboot(String reason) 重啓設備 Reboot the device. |
|
void |
userActivity(long when, boolean noChangeLights) 用戶界面發生。 User activity happened. |