天天看點

android PowerManager

android.os.PowerManager

通過PowerManager類我們可以對裝置的電源進行管理。對該類API的使用将影響到電池壽命。隻有在必須使用WakeLocks的時候,才使用WakeLocks,且在不使用它的時候要及時釋放(release).

圖一:

android PowerManager

預設情況下,當使用者對手機有一段時間沒有操作後,手機的Keyboard(這裡不僅僅指硬鍵盤,還包括其他的所有鍵,比如Menu)背光将消失,從Bright變為Off,如果再過段時間沒操作,螢幕(Screen)将從高亮(Bright)變為暗淡(Dim),如果再過段時間沒操作,螢幕(Screen)将又由暗淡(Dim)變為不顯示(Off),如果再過段時間沒操作,CPU将sleep,從on變為off.通過PowerManager類可以對上述過程進行管理,可以讓裝置到達上面的某種狀态時,該狀态将不再逾時,将不再往下走,但是仍然可以跳到到更上級的某種狀态(比如使用者有活動,可以讓手機回到最高狀态)。

比如,示例1:

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

 PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");

 wl.acquire();

   ..screen will stay on during this section..

 wl.release();

你可以如下的flag,來說明将進行怎樣的電源管理。下面的flag都是互斥,你隻有使用其中的一個。

Flag Value                   CPU     Screen      Keyboard 

PARTIAL_WAKE_LOCK            On*      Off          Off 

SCREEN_DIM_WAKE_LOCK         On       Dim          Off 

SCREEN_BRIGHT_WAKE_LOCK      On       Bright       Off 

FULL_WAKE_LOCK               On       Bright       Bright

如果你hold了一個partial wakelock,那麼CPU将一直運作,甚至在使用者按下電源按鈕。 對于其他的wakelocks,那麼CPU将繼續運作,但是使用者可以通過按下電源按鈕來停止CPU的運作。我們可以建立多個鎖,并hold它,即使對同一類型,也如此,對于某類型的wakelock隻要有一個被hold,那麼它所對應的電源狀态(illumination),就将不會逾時,将被延續(hold).在上表中我們把越往下的,稱為更高一級的wakelocks.當進階和低級wakelocks相遇的時候,進階起作用。

在上面的flag上還再加上如下的2個flag,但是他們和PARTIAL_WAKE_LOCK.組合沒任何意義

ACQUIRE_CAUSES_WAKEUP

預設情況下wake locks并不是馬上開啟CPU或Screen或Keyboard的illumination(對于Screen是Dim或Bright,Keyboard是Bright. wake locks隻是在被開啟後(比如使用者的活動),讓裝置延續(儲存)你設定開啟的狀态. 但是如果加上ACQUIRE_CAUSES_WAKEUP就可以讓Screen或Keyboar的illumination沒開啟的情況,馬上開啟它們。 典型的應用就是在收到一個重要的notifications時,需要馬上點亮螢幕。

ON_AFTER_RELEASE

如果有該flag, 那麼在WakeLock被釋放的時候,user activity計時器将被重設, 這樣illumination将持續一段更長的時間.This can be used to reduce flicker if you are cycling between wake lock conditions.

主要函數:

public void goToSleep (long time)

Since: API Level 1

Force the device to go to sleep. Overrides all the wake locks that are held.

參數

time is used to order this correctly with the wake lock calls. The time should be in the SystemClock.uptimeMillis() time base.

該函數用于強制讓裝置進入休眠狀态。

注意:在Eclipse環境中我使用該函數運作失敗.可能需要在核心編譯的環境中使用才行。

public boolean isScreenOn ()

Since: API Level 7

Returns whether the screen is currently on. The screen could be bright or dim.

用于判斷螢幕是否處于點亮狀态(包括Bright和dim)

示例2:

 boolean isScreenOn = pm.isScreenOn();

public PowerManager.WakeLock newWakeLock (int flags, String tag)

建立一個flag所指定的類型的wake lock對象,可以通過調用該對象的acquire()方法在獲得一個wake鎖, 使用完後可以通過release() 釋放該鎖

示例3:

PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);

PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK|PowerManager.ON_AFTER_RELEASE,

                                      TAG);

wl.acquire();

 // ...

wl.release();

flags Combination of flag values defining the requested behavior of the WakeLock.

用于指定建立的wake lock對象的類型

tag Your class name (or other tag) for debugging purposes.用于标記是在哪個地方建立的該wake lock對象,以便調試用。

public void reboot (String reason)

Since: API Level 8

Reboot the device. Will not return if the reboot is successful. Requires the REBOOT permission.

reason code to pass to the kernel (e.g., "recovery") to request special boot modes, or null.

該函數用于重新開機裝置。

public void userActivity (long when, boolean noChangeLights)

User activity happened.

Turns the device from whatever state it's in to full on, and resets the auto-off timer.

when is used to order this correctly with the wake lock calls. This time should be in the SystemClock.uptimeMillis() time base.

noChangeLights should be true if you don't want the lights to turn on because of this event. This is set when the power key goes down. We want the device to stay on while the button is down, but we're about to turn off. Otherwise the lights flash on and then off and it looks weird.

該函數主要就是用于通知系統有個user Activity發生了 ,when就是指user Activity發生的時間,該時間是基于SystemClock.uptimeMillis(),noChangeLights是指是否需要因為該user Activity而把Screen和Keyboard的Lights點亮。當我們按下power鍵要進行關屏的時候,就不需要點亮Screen和Keyboard的Lights,是以該參數為true,否則的話,先點亮Screen和Keyboard的Lights,然後再關掉螢幕,就很奇怪了。

需要的permission

需要在AndroidManifest.xml中加入以下2個permission:

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

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

繼續閱讀