平台:msm8953 Android N
一、现象
手机连接电脑reboot,进入系统,终端使用命令adb shell dumpsys power查看信息有个wake lock锁,具体如下:
Wake Locks: size=2
PARTIAL_WAKE_LOCK ‘ActivityManager-Sleep’ ACQ=-6m22s755ms LONG (uid=1000 pid=1511)
DOZE_WAKE_LOCK ‘DreamManagerService’ ACQ=-6m11s170ms (uid=1000 pid=1511)
Suspend Blockers: size=4
PowerManagerService.WakeLocks: ref count=1
PowerManagerService.Display: ref count=0
PowerManagerService.Broadcasts: ref count=0
PowerManagerService.WirelessChargerDetector: ref count=0
Display Power: state=OFF
二、分析
首先根据log可以看出是“ActivityManager-Sleep”相关的wakelock没有被释放,查找到“ActivityManager-Sleep”所在定义:
/frameworks/base/services/core/java/com/android/server/am/ActivityStackSupervisor.java
PowerManager.WakeLock mGoingToSleep;
/**
* At the time when the constructor runs, the power manager has not yet been
* initialized. So we initialize our wakelocks afterwards.
*/
void initPowerManagement() {
PowerManager pm = (PowerManager)mService.mContext.getSystemService(Context.POWER_SERVICE);
mGoingToSleep = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "ActivityManager-Sleep");//在此处定义
mLaunchingActivity = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "*launch*");
mLaunchingActivity.setReferenceCounted(false);
}
可以看到是mGoingToSleep这个wakelock,接下来查找mGoingToSleep的acquire:
void goingToSleepLocked() {
scheduleSleepTimeout();
if (!mGoingToSleep.isHeld()) {
**mGoingToSleep.acquire();**