首先来看一下,在ShutdownThread里面有一个CloseDialogReceiver来关注Intent.ACTION_CLOSE_SYSTEM_DIALOGS,它收到这个消息就会关闭这个对话框。对话框怎么起来的呢?请看下面的源码: [java] view plain copy
- if (confirm) {
- final CloseDialogReceiver closer = new CloseDialogReceiver(context);
- final AlertDialog dialog = new AlertDialog.Builder(context)
- .setTitle(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);
- }
- })
- .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);
- }
其实就是一个AlertDialog,也没什么新鲜的,只是在setPositiveButton的时候注册了clicklistener来监听你是否按下了,按下了就直接执行beginShutdownSequence。在beginShutdownSequence还会弹出一个进度的对话框,代码如下: [java] view plain copy
- ProgressDialog pd = new ProgressDialog(context);
- pd.setTitle(context.getText(com.android.internal.R.string.power_off));
- pd.setMessage(context.getText(com.android.internal.R.string.shutdown_progress));
- pd.setIndeterminate(true);
- pd.setCancelable(false);
- pd.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
-
- pd.show();
在里面还会调用两个非常重要的Power.shutdown()跟Power.reboot(reason),看你是重启还是关机了。 [cpp] view plain copy
- /**
- * Low-level function turn the device off immediately, without trying
- * to be clean. Most people should use
- * {@link android.internal.app.ShutdownThread} for a clean shutdown.
- *
- * @deprecated
- * @hide
- */
- @Deprecated
- public static native void <span style="color:#ff0000;">shutdown</span>();
-
- /**
- * 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 reboot(String reason) throws IOException
- {
- <span style="color:#ff0000;">rebootNative</span>(reason);
- }
-
- private static native void rebootNative(String reason) throws IOException ;
再往下跟,
[cpp] view plain copy
- static void android_os_Power_shutdown(JNIEnv *env, jobject clazz)
- {
- android_reboot(ANDROID_RB_POWEROFF, 0, 0);
- }
-
- extern int go_recovery(void);
-
- static void android_os_Power_reboot(JNIEnv *env, jobject clazz, jstring reason)
- {
- if (reason == NULL) {
- android_reboot(ANDROID_RB_RESTART, 0, 0);
- } else {
- const char *chars = env->GetStringUTFChars(reason, NULL);
- //android_reboot(ANDROID_RB_RESTART2, 0, (char *) chars);
- go_recovery();
- android_reboot(ANDROID_RB_RESTART, 0, 0);
- env->ReleaseStringUTFChars(reason, chars); // In case it fails.
- }
- jniThrowIOException(env, errno);
- }
所以,整个流程都是好的,学习理了一下流程,大部分都是源码,把它搞清楚也是有好处的。
|