1. 项目背景
在项目的开发过程中,往往会遇到这样的问题:Android系统关机慢,关机过程中黑屏时间长。
2. 代码流程
(1) PowerManagerService.java
/**
* Reboots the device.
*
* @param confirm If true, shows a reboot confirmation dialog.
* @param reason The reason for the reboot, or null if none.
* @param wait If true, this call waits for the reboot to complete and does not return.
*/
@Override // Binder call
public void reboot(boolean confirm, @Nullable String reason, boolean wait) {
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null);
if (PowerManager.REBOOT_RECOVERY.equals(reason)
|| PowerManager.REBOOT_RECOVERY_UPDATE.equals(reason)) {
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.RECOVERY, null);
}
ShutdownCheckPoints.recordCheckPoint(Binder.getCallingPid(), reason);
final long ident = Binder.clearCallingIdentity();
try {
shutdownOrRebootInternal(HALT_MODE_REBOOT, confirm, reason, wait);
} finally {
Binder.restoreCallingIdentity(ident);
}
}
3736 private void shutdownOrRebootInternal(final @HaltMode int haltMode, final boolean confirm,
3737 @Nullable final String reason, boolean wait) {
3738 if (PowerManager.REBOOT_USERSPACE.equals(reason)) {
3739 if (!PowerManager.isRebootingUserspaceSupportedImpl()) {
3740 throw new UnsupportedOperationException(
3741 "Attempted userspace reboot on a device that doesn't support it");
3742 }
3743 UserspaceRebootLogger.noteUserspaceRebootWasRequested();
3744 }
3745 if (mHandler == null || !mSystemReady) {
3746 if (RescueParty.isAttemptingFactoryReset()) {
3747 // If we're stuck in a really low-level reboot loop, and a
3748 // rescue party is trying to prompt the user for a factory data
3749 // reset, we must GET TO DA CHOPPA!
3750 // No check point from ShutdownCheckPoints will be dumped at this state.
3751 PowerManagerService.lowLevelReboot(reason);
3752 } else {
3753 throw new IllegalStateException("Too early to call shutdown() or reboot()");
3754 }
3755 }
3756
3757 Runnable runnable = new Runnable() {
3758 @Override
3759 public void run() {
3760 synchronized (this) {
3761 if (haltMode == HALT_MODE_REBOOT_SAFE_MODE) {
3762 ShutdownThread.rebootSafeMode(getUiContext(), confirm);
3763 } else if (haltMode == HALT_MODE_REBOOT) {
3764 ShutdownThread.reboot(getUiContext(), reason, confirm);
3765 } else {
3766 ShutdownThread.shutdown(getUiContext()