Android系统关机的全流程解析

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

如果确认重启,则调用PowerManagerService的lowLevelReboot函数,参数就是传递下来的reason,稍后分析。如果不是重启,即mReboot=false,那就是需要关机了,在shutdown函数中就能够知道。

/** 
 * 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 confirm true if user confirmation is needed before shutting down. 
 */ 
public static void shutdown(final Context context, boolean confirm) { 
  mReboot = false; 
  mRebootSafeMode = false; 
  shutdownInner(context, confirm); 
} 

关机的时候需要震动,就是这里了SHUTDOWN_VIBRATE_MS,默认的定义是500ms。但是在代码上看,无论如何,最后都会调用一下lowLevelShutdown函数,也就是关机。逻辑上,这里可能是个问题,但是实际中,如果重启操作能够调用成功的话,整个系统都重启了,后边的代码当然不可能执行到了。
目光转回PowerManagerService
4.frameworks/base/services/java/com/android/server/PowerManagerService.java

/**  
 * Low-level function to reboot the device. 
 * 
 * @param reason code to pass to the kernel (e.g. "recovery"), or null. 
 * @throws IOException if reboot fails for some reason (eg, lack of 
 *     permission) 
 */ 
public static void lowLevelReboot(String reason) throws IOException { 
  nativeReboot(reason); 
}  
 
/**  
 * Low-level function turn the device off immediately, without trying 
 * to be clean. Most people should use 
 * {@link com.android.server.pm.internal.app.ShutdownThread} for a clean shutdown. 
 */ 
public static void lowLevelShutdown() { 
  nativeShutdown(); 
} 

 

很熟悉的字样native,是JNI调用了: