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(); }
可以发现, 基本都是接受到一些系统广播之后进行调用(以handle开头的方法), 以及系统设置后调用(set开头的方法), 来更新电源状态, 还有一些接口回调, 主要是给用户的一些操作
先是public的回调方法, 回调方法把任务传递到内部的private方法(Internal结尾), 还有接收的native方法(Native结尾), 最后都是通过Locked结尾的方法调用updatePowerStateLocked()更新状态.