关机动作从按键触发中断,linux kernel层给android framework层返回按键事件进入 framework层,
再从 framework层到kernel层执行kernel层关机任务。长按键对应的handler代码:
frameworks\policies\base\phone\com\android\internal\policy\impl\phonewindowmanager.java
- Runnable mPowerLongPress;
- private final Runnable mPowerLongPress = new Runnable() {
- public void run() {
- if (!mPowerKeyHandled) {
- mPowerKeyHandled = true;
- performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
- sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
- showGlobalActionsDialog();
- }
- }
- };
调用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);
- public static void reboot(final Context context, String reason, boolean confirm) {
- mReboot = true;
- mRebootSafeMode = false;
- mRebootReason = reason;
- shutdownInner(context, confirm);
- }
shutdownInner函數如下,其調用了beginShutdownSequence(context);
- static void shutdownInner(final Context context, boolean confirm) {
- ....
- if (confirm) {
- final CloseDialogReceiver closer = new CloseDialogReceiver(context);
- final AlertDialog dialog = new AlertDialog.Builder(context)
- .setTitle(mRebootSafeMode
- ? com.android.internal.R.string.reboot_safemode_title
- : com.android.internal.R.string.power_off)
- .setMessage(resourceId)
- .setPositiveButton(com.android.internal.R.string.yes, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- beginShutdownSequence(context);//調用beginShutdownSequence
- }
- })
- .setNegativeButton(com.android.internal.R.string.no, null)
- .create();
- closer.dialog = dialog;
- dialog.setOnDismissListener(closer);
- dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
- dialog.show();
- } else {
- beginShutdownSequence(context);
- }
- }
beginShutdownSequence如下
- private static void beginShutdownSequence(Context context) {
- ......
- sInstance.start();//從這兒就會開始調用到run()方法
- }
在run()方法中也會做一些reboot之前的清除工作,关掉要关的服务等。
- public void run() {
- BroadcastReceiver br = new BroadcastReceiver() {
- @Override public void onReceive(Context context, Intent intent) {
- // We don't allow apps to cancel this, so ignore the result.
- actionDone();
- }
- };
- ......省略
- // Set initial variables and time out time.
- mActionDone = false;
- final long endShutTime = SystemClock.elapsedRealtime() + MAX_SHUTDOWN_WAIT_TIME;
- synchronized (mActionDoneSync) {
- try {
- final IMountService mount = IMountService.Stub.asInterface(
- ServiceManager.checkService("mount"));
- if (mount != null) {
- mount.shutdown(observer);
- } else {
- Log.w(TAG, "MountService unavailable for shutdown");
- }
- } catch (Exception e) {
- Log.e(TAG, "Exception during MountService shutdown", e);
- }
- while (!mActionDone) {
- long delay = endShutTime - SystemClock.elapsedRealtime();
- if (delay <= 0) {
- Log.w(TAG, "Shutdown wait timed out");
- break;
- }
- try {
- mActionDoneSync.wait(delay);
- } catch (InterruptedException e) {
- }
- }
- }
- rebootOrShutdown(mReboot, mRebootReason);//又繞到rebootOrShutdown函數