標籤:
Android的待機狀態管理由PowerManagerService.java管理
主要的狀態更新方法在下面貼出代碼, 注釋寫的很清楚, 第一次看系統源碼感覺還比較爽
主要是更新喚醒, 螢幕休眠以及其他的一些狀態, 然後系統根據更新的狀態進行一些操作, 比如系統睡眠, 進入屏保, 或者電源模式變更等等.
/** * Updates the global power state based on dirty bits recorded in mDirty. * * This is the main function that performs power state transitions. * We centralize them here so that we can recompute the power state completely * each time something important changes, and ensure that we do it the same * way each time. The point is to gather all of the transition logic here. */ private void updatePowerStateLocked() { if (!mSystemReady || mDirty == 0) { return; } if (!Thread.holdsLock(mLock)) { Slog.wtf(TAG, "Power manager lock was not held when calling updatePowerStateLocked"); } // Phase 0: Basic state updates. updateIsPoweredLocked(mDirty); updateStayOnLocked(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 dreams and display power state. updateDreamLocked(dirtyPhase2); updateDisplayPowerStateLocked(dirtyPhase2); // Phase 3: Send notifications, if needed. if (mDisplayReady) { sendPendingNotificationsLocked(); } // Phase 4: Update suspend blocker. // Because we might release the last suspend blocker here, we need to make sure // we finished everything else first! updateSuspendBlockerLocked(); }
調用updatePowerStateLocked()方法的地方有:
可以發現, 基本都是接受到一些系統廣播之後進行調用(以handle開頭的方法), 以及系統設定後調用(set開頭的方法), 來更新電源狀態, 還有一些介面回調, 主要是給使用者的一些操作
先是public的回調方法, 回調方法把任務傳遞到內部的private方法(Internal結尾), 還有接收的native方法(Native結尾), 最後都是通過Locked結尾的方法調用updatePowerStateLocked()更新狀態.
Android待機狀態更新