Android WakeLock電源管理

前言

         歡迎大傢我分享和推薦好用的代碼段~~

聲明

         歡迎轉載,但請保留文章原始出處:

         CSDN:https://www.csdn.net

         雨季o莫憂離:https://blog.csdn.net/luckkof

正文

 

PowerManager.WakeLock

  PowerManager.WakerLock是我分析Standup Timer源代碼時發現的一個小知識點,Standup Timer 用WakeLock保證程序運行時保持手機屏幕的恒亮(程序雖小但也做得相當的細心,考慮的很周到)。PowerManager 和PowerManager.WakerLock7用於對Android設備的電源進行管理。

  PowerManager:This class gives you control of the power state of the device.

  PowerManager.WakeLock: lets you say that you need to have the device on.

  Android中通過各種Lock鎖對電源進行控制,需要註意的是加鎖和解鎖必須成對出現。先上一段Standup Timer裡的代碼然後進行說明。

[java] 

代碼   

  

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;  

}  

  

}  

 

acquireWakeLock()方法中獲取瞭 SCREEN_DIM_WAKE_LOCK鎖,該鎖使 CPU 保持運轉,屏幕保持亮度(可以變灰)。這個函數在Activity的 onResume中被調用。releaseWakeLock()方法則是釋放該鎖。它在Activity的 onPause中被調用。利用Activiy的生命周期,巧妙的讓 acquire()和release()成對出現。

[java] 

@Override  

protectedvoid onResume()  

{  

super.onResume();  

//獲取鎖,保持屏幕亮度  

acquireWakeLock();  

startTimer();  

}  

 

[java] view plaincopy

代碼   

  

  

protectedvoid onPause()  

{  

super.onPause();  

synchronized(this) {  

cancelTimer();  

releaseWakeLock();  

  

if (finished) {  

clearState();  

} else {  

saveState();  

}  

}  

}  

 

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中聲明該應用有設置電源管理的權限。

[html]  

<uses-permission android:name="android.permission.WAKE_LOCK"/>  

你可能還需要  

<uses-permission android:name="android.permission.DEVICE_POWER"/>  

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *