Android关机流程 一

来源:互联网 发布:优化资源配置 编辑:程序博客网 时间:2024/06/06 09:29

关机动作从按键触发中断,linux kernel层给android framework层返回按键事件进入 framework层,

再从 framework层到kernel层执行kernel层关机任务。长按键对应的handler代码:

frameworks\policies\base\phone\com\android\internal\policy\impl\phonewindowmanager.java

[cpp] view plaincopyprint?
  1. Runnable mPowerLongPress;  
  2. private final Runnable mPowerLongPress = new Runnable() {  
  3.         public void run() {  
  4.             if (!mPowerKeyHandled) {  
  5.                 mPowerKeyHandled = true;  
  6.                 performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);  
  7.                 sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);  
  8.                 showGlobalActionsDialog();  
  9.             }  
  10.         }  
  11.     };  

调用showglobalactionsdialog() 方法将会显示上面提到的显示 “飞行模式” ,“ 静音” ,“ ” 关机 ,

选项的对话框。


mGlobalActions.showDialog(keyguardShowing, isDeviceProvisioned())该函数是 

mGlobalActions.showDialog()函数定义在frameworks/base/policy/src/com/android/internal/policy/impl/GlobalActions.java文件



其中会调用 createDialog()

private AlertDialog createDialog()中


根据平台不同这里有差异,在createDialog()中

调用mWindowManagerFuncs.shutdown();

此函数在frameworks/base/services/java/com/android/server/pm/ShutdownThread.java实现

此函数中调用shutdownInner(context,confirm); 

[cpp] view plaincopyprint?
  1. public static void reboot(final Context context, String reason, boolean confirm) {    
  2.        mReboot = true;    
  3.        mRebootSafeMode = false;    
  4.        mRebootReason = reason;    
  5.        shutdownInner(context, confirm);    
  6.    }    

shutdownInner函數如下,其調用了beginShutdownSequence(context);

[cpp] view plaincopyprint?
  1. static void shutdownInner(final Context context, boolean confirm) {    
  2.       ....    
  3.  if (confirm) {    
  4.             final CloseDialogReceiver closer = new CloseDialogReceiver(context);    
  5.             final AlertDialog dialog = new AlertDialog.Builder(context)    
  6.                     .setTitle(mRebootSafeMode    
  7.                             ? com.android.internal.R.string.reboot_safemode_title    
  8.                             : com.android.internal.R.string.power_off)    
  9.                     .setMessage(resourceId)    
  10.                     .setPositiveButton(com.android.internal.R.string.yes, new DialogInterface.OnClickListener() {    
  11.                         public void onClick(DialogInterface dialog, int which) {    
  12.                             beginShutdownSequence(context);//調用beginShutdownSequence    
  13.                         }    
  14.                     })    
  15.                     .setNegativeButton(com.android.internal.R.string.no, null)    
  16.                     .create();    
  17.             closer.dialog = dialog;    
  18.             dialog.setOnDismissListener(closer);    
  19.             dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);    
  20.             dialog.show();    
  21.         } else {    
  22.             beginShutdownSequence(context);    
  23.         }    
  24.     }    

beginShutdownSequence如下

[cpp] view plaincopyprint?
  1. private static void beginShutdownSequence(Context context) {    
  2.            
  3.     ......    
  4.        sInstance.start();//從這兒就會開始調用到run()方法    
  5.    }    

在run()方法中也會做一些reboot之前的清除工作,关掉要关的服务等。

[cpp] view plaincopyprint?
  1. public void run() {    
  2.        BroadcastReceiver br = new BroadcastReceiver() {    
  3.            @Override public void onReceive(Context context, Intent intent) {    
  4.                // We don't allow apps to cancel this, so ignore the result.    
  5.                actionDone();    
  6.            }    
  7.        };    
  8.     
  9.              ......省略    
  10.        // Set initial variables and time out time.    
  11.        mActionDone = false;    
  12.        final long endShutTime = SystemClock.elapsedRealtime() + MAX_SHUTDOWN_WAIT_TIME;    
  13.        synchronized (mActionDoneSync) {    
  14.            try {    
  15.                final IMountService mount = IMountService.Stub.asInterface(    
  16.                       ServiceManager.checkService("mount"));    
  17.                if (mount != null) {    
  18.                    mount.shutdown(observer);    
  19.               } else {    
  20.                    Log.w(TAG, "MountService unavailable for shutdown");    
  21.                }    
  22.            } catch (Exception e) {    
  23.                Log.e(TAG, "Exception during MountService shutdown", e);    
  24.            }    
  25.            while (!mActionDone) {    
  26.                long delay = endShutTime - SystemClock.elapsedRealtime();    
  27.                if (delay <= 0) {    
  28.                    Log.w(TAG, "Shutdown wait timed out");    
  29.                    break;    
  30.                }    
  31.                try {    
  32.                    mActionDoneSync.wait(delay);    
  33.                } catch (InterruptedException e) {    
  34.                }    
  35.            }    
  36.       }    
  37.     
  38.        rebootOrShutdown(mReboot, mRebootReason);//又繞到rebootOrShutdown函數
0 0