WakeLock 实施概述
当我们使用pm.newWakeLock 创建一个新的唤醒锁时,PowerManager 只是创建一个新的 WakeLock 对象并返回。 WakeLock 对象不是 binder 对象,因此不能通过多个进程使用。但是,在该 WakeLock 对象中,它包含一个名为 mToken 的 Binder 对象。
WakeLock(int flags, String tag) {
mFlags = flags;
mTag = tag;
mToken = new Binder();
}
因此,当您在此 WakeLock 对象上调用获取或释放时,它实际上将该令牌传递给 PowerManagerService。
private void acquireLocked() {
if (!mRefCounted || mCount++ == 0) {
mHandler.removeCallbacks(mReleaser);
try {
mService.acquireWakeLock(mToken, mFlags, mTag, mWorkSource);
} catch (RemoteException e) {
}
mHeld = true;
}
}
查看PowerManagerService 在获取或释放唤醒锁时的工作方式将帮助您回答您的问题。
void acquireWakeLockInternal(IBinder lock, int flags, String tag, WorkSource ws,
int uid, int pid) {
synchronized (mLock) {
...
WakeLock wakeLock;
int index = findWakeLockIndexLocked(lock);
if (index >= 0) {
...
// Update existing wake lock. This shouldn't happen but is harmless.
...
} else {
wakeLock = new WakeLock(lock, flags, tag, ws, uid, pid);
try {
lock.linkToDeath(wakeLock, 0);
} catch (RemoteException ex) {
throw new IllegalArgumentException("Wake lock is already dead.");
}
notifyWakeLockAcquiredLocked(wakeLock);
mWakeLocks.add(wakeLock);
}
...
}
...
}
关键语句是lock.linkToDeath(wakeLock, 0);。那lock正是我们之前提到的mToken。如果此活页夹消失,此方法将注册收件人(wakeLock)以获取通知。如果这个 binder 对象意外消失(通常是因为它的宿主进程已被杀死),那么将在接收者上调用 binderDied 方法。
注意PowerManagerService 中的WakeLock 与PowerManager 中的WakeLock 不同,它是IBinder.DeathRecipient 的实现。所以看看它的binderDied 方法。
@Override
public void binderDied() {
PowerManagerService.this.handleWakeLockDeath(this);
}
handleWakeLockDeath 将释放该唤醒锁。
private void handleWakeLockDeath(WakeLock wakeLock) {
synchronized (mLock) {
...
int index = mWakeLocks.indexOf(wakeLock);
if (index < 0) {
return;
}
mWakeLocks.remove(index);
notifyWakeLockReleasedLocked(wakeLock);
applyWakeLockFlagsOnReleaseLocked(wakeLock);
mDirty |= DIRTY_WAKE_LOCKS;
updatePowerStateLocked();
}
}
所以我认为在您的问题中的两种情况下,答案都是不用担心。至少在 Android 4.2(代码来自哪里)中,这是真的。此外,PowerManager 中的 WakeLock 类有一个 finalize 方法,但这不是您问题的关键。