Android電源管理系列之PowerManagerService(二)
WakeLock機制
PowerManager.WakeLock
為了延長電池的使用壽命,Android裝置會在一段時間後使螢幕變暗,然後關閉螢幕顯示,直至停止CPU進入休眠。WakeLock是Android提供的喚醒鎖機制,用來保持CPU執行或避免螢幕變暗/關閉以及避免鍵盤背光燈熄滅
喚醒鎖的型別:
Flag | 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 | on |
如果是PARTIAL_WAKE_LOCK,無論螢幕的狀態或是按下電源鍵, CPU都將正常工作。如果是其它的喚醒鎖,裝置會在使用者按下電源鈕後停止工作進入休眠狀態。以上四種鎖,除了PARTIAL_WAKE_LOCK,其餘的鎖在API level 17已經被deprecated了。
喚醒鎖的使用方法
程式碼使用:
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE); PowerManager.WakeLock wl =powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag"); wl.acquire(); //acquire時儘量申明timeout時間 // ... do work... wl.release();
許可權申明:
<uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.DEVICE_POWER"/>
在應用程式中使用WakeLock時必須申明許可權,acquire請求喚醒鎖時儘量設定timeout時間釋放WakeLock,以避免長時間持有WakeLock導致系統無法休眠。
喚醒鎖的實現:
frameworks/base/core/java/android/os/PowerManager.java
public final class WakeLock { private int mFlags; private String mTag; private final String mPackageName; private final IBinder mToken; private int mCount; private boolean mRefCounted = true; private boolean mHeld; private WorkSource mWorkSource; private String mHistoryTag; private final String mTraceName; ...... WakeLock(int flags, String tag, String packageName) { mFlags = flags; mTag = tag; mPackageName = packageName; mToken = new Binder(); mTraceName = "WakeLock (" + mTag + ")"; } ...... }
updatePowerStateLocked
frameworks/base/services/core/java/com/android/server/power/PowerManagerService.java
protected void updatePowerStateLocked() { // 服務沒有ready,mDirty值沒有設定情況下不做更新操作 if (!mSystemReady || mDirty == 0) { return; } if (!Thread.holdsLock(mLock)) { Slog.wtf(TAG, "Power manager lock was not held when calling updatePowerStateLocked"); } try { // Phase 0: 更新基本狀態 updateIsPoweredLocked(mDirty); updateStayOnLocked(mDirty); updateScreenBrightnessBoostLocked(mDirty); // Phase 1: Update wakefulness. // Loop because the wake lock and user activity computations are influenced // by changes in wakefulness. final long now = SystemClock.uptimeMillis(); int dirtyPhase2 = 0; for (;;) { int dirtyPhase1 = mDirty; dirtyPhase2 |= dirtyPhase1; mDirty = 0; updateWakeLockSummaryLocked(dirtyPhase1); updateUserActivitySummaryLocked(now, dirtyPhase1); if (!updateWakefulnessLocked(dirtyPhase1)) { break; } } // Phase 2: Update display power state. boolean displayBecameReady = updateDisplayPowerStateLocked(dirtyPhase2); // Phase 3: Update dream state (depends on display ready signal). updateDreamLocked(dirtyPhase2, displayBecameReady); // Phase 4: Send notifications, if needed. finishWakefulnessChangeIfNeededLocked(); // Phase 5: Update suspend blocker. // Because we might release the last suspend blocker here, we need to make sure // we finished everything else first! updateSuspendBlockerLocked(); } finally { Trace.traceEnd(Trace.TRACE_TAG_POWER); } }
首先來看updateIsPoweredLocked(mDirty);
private void updateIsPoweredLocked(int dirty) { if ((dirty & DIRTY_BATTERY_STATE) != 0) { final boolean wasPowered = mIsPowered; final int oldPlugType = mPlugType; final boolean oldLevelLow = mBatteryLevelLow; mIsPowered = mBatteryManagerInternal.isPowered(BatteryManager.BATTERY_PLUGGED_ANY); mPlugType = mBatteryManagerInternal.getPlugType(); mBatteryLevel = mBatteryManagerInternal.getBatteryLevel(); mBatteryLevelLow = mBatteryManagerInternal.getBatteryLevelLow(); if (wasPowered != mIsPowered || oldPlugType != mPlugType) { mDirty |= DIRTY_IS_POWERED; // Update wireless dock detection state. final boolean dockedOnWirelessCharger = mWirelessChargerDetector.update( mIsPowered, mPlugType, mBatteryLevel); // Treat plugging and unplugging the devices as a user activity. // Users find it disconcerting when they plug or unplug the device // and it shuts off right away. // Some devices also wake the device when plugged or unplugged because // they don't have a charging LED. final long now = SystemClock.uptimeMillis(); if (shouldWakeUpWhenPluggedOrUnpluggedLocked(wasPowered, oldPlugType, dockedOnWirelessCharger)) { wakeUpNoUpdateLocked(now, "android.server.power:POWER", Process.SYSTEM_UID, mContext.getOpPackageName(), Process.SYSTEM_UID); } userActivityNoUpdateLocked( now, PowerManager.USER_ACTIVITY_EVENT_OTHER, 0, Process.SYSTEM_UID); // Tell the notifier whether wireless charging has started so that // it can provide feedback to the user. //Bug293654, zhanghong.wt, modify, 20170914, modify no notificatin ring while plugging USB if (dockedOnWirelessCharger || (mIsPowered && oldPlugType != mPlugType&&("1".equals(SystemProperties.get("sys.boot_completed"))))) { mNotifier.onWirelessChargingStarted(); } } if (wasPowered != mIsPowered || oldLevelLow != mBatteryLevelLow) { if (oldLevelLow != mBatteryLevelLow && !mBatteryLevelLow) { if (DEBUG_SPEW) { Slog.d(TAG, "updateIsPoweredLocked: resetting low power snooze"); } mAutoLowPowerModeSnoozing = false; } updateLowPowerModeLocked(); } } }
最後附上總結圖
PowerManagerService 總結圖
至此,本篇已結束,如有不對的地方,歡迎您的建議與指正。期待您的關注,
感謝您的閱讀,謝謝!
如有侵權,請聯絡小編,小編對此深感抱歉,同時小編會立即停止侵權行為。
公眾號ID:ProgramAndroid
獲取更多資訊
微信公眾號:ProgramAndroid
我們不是牛逼的程式設計師,我們只是程式開發中的墊腳石。
我們不傳送紅包,我們只是紅包的搬運工。
點選閱讀原文,獲取更多福利