Android系统关机的全流程解析

2019-12-10 19:02:04刘景俊

3.frameworks/base/services/java/com/android/server/PowerManagerService.java

/**  
 * Reboot the device immediately, passing 'reason' (may be null) 
 * to the underlying __reboot system call. Should not return. 
 */ 
public void reboot(String reason) 
{   
  mContext.enforceCallingOrSelfPermission(android.Manifest.permission.REBOOT, null); 
 
  if (mHandler == null || !ActivityManagerNative.isSystemReady()) { 
    throw new IllegalStateException("Too early to call reboot()"); 
  }   
 
  final String finalReason = reason; 
  Runnable runnable = new Runnable() { 
    public void run() { 
      synchronized (this) { 
        ShutdownThread.reboot(getUiContext(), finalReason, false); 
      }   
 
    }   
  };   
  // ShutdownThread must run on a looper capable of displaying the UI. 
  mHandler.post(runnable); 
 
  // PowerManager.reboot() is documented not to return so just wait for the inevitable. 
  synchronized (runnable) { 
    while (true) { 
      try { 
        runnable.wait(); 
      } catch (InterruptedException e) {  
      }   
    }   
  }   
} 

4.frameworks/base/services/java/com/android/server/pm/ShutdownThread.java

/** 
 * Request a clean shutdown, waiting for subsystems to clean up their 
 * state etc. Must be called from a Looper thread in which its UI 
 * is shown. 
 * 
 * @param context Context used to display the shutdown progress dialog. 
 * @param reason code to pass to the kernel (e.g. "recovery"), or null. 
 * @param confirm true if user confirmation is needed before shutting down. 
 */ 
public static void reboot(final Context context, String reason, boolean confirm) { 
  mReboot = true; 
  mRebootSafeMode = false; 
  mRebootReason = reason; 
  shutdownInner(context, confirm); 
} 

这里说明是需要重启,且不是安全模式,重启参数为传递下来的reason,shutdownInner的confirm参数是用来设置是否有确认提示框的,通过reboot接口调用重启是没有的,为false。