android reboot 功能的添加

最近做一款手机,需要添加一个手机重启功能,后来查看源码发现,代码中有reboot功能,只需要我们添加可视的界面就可以了,现在讲我添加的代码复制在上面,方便以后查阅。

添加这个功能主要用到了两个类:

   frameworks\base\policy\src\com\android\internal\policy\impl\GlobalActions.java

frameworks\base\core\java\com\android\internal\app\ShutdowThread.java


1.首先改GlobalActions.java

   我们在CreateDialog()方法里面的:new SinglePressAction前面添加

       new SinglePressAction(
                        com.android.internal.R.drawable.ic_lock_reboot,
                        R.string.global_action_reboot) {

                    public void onPress() {
                        // shutdown by making sure radio and power are handled accordingly.
                        ShutdownThread.reboot(mContext, "click",true);
                    }

                    public boolean showDuringKeyguard() {
                        return true;
                    }

                    public boolean showBeforeProvisioning() {
                        return true;
                    }
                },

其中上面的代码是copy第一个new SinglePressAction的。只是需要把里面的资源换成重启的东西。

2.修改ShutdownThread.java

    添加下面方法:

                       private static void reboot(final Context context, boolean confirm) {
        // ensure that only one thread is trying to power down.
        // any additional calls are just returned

        Log.d(TAG, "!!! Request to shutdown !!!");

        if (SystemProperties.getBoolean("ro.monkey", false)) {
            Log.d(TAG, "Cannot request to shutdown when Monkey is running, returning.");
            return;
        }

        synchronized (sIsStartedGuard) {
            if (sIsStarted) {
                Log.d(TAG, "Request to shutdown already running, returning.");
                return;
            }
        }

        Log.d(TAG, "Notifying thread to start radio shutdown");
        bConfirmForAnimation = confirm;
        if (confirm) {
            final AlertDialog dialog = new AlertDialog.Builder(context)
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setTitle(com.android.internal.R.string.reboot)
                    .setMessage(com.android.internal.R.string.reboot_confirm)
                    .setPositiveButton(com.android.internal.R.string.yes, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            beginShutdownSequence(context,false);
                        }
                    })
                    .setNegativeButton(com.android.internal.R.string.no, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            synchronized (sIsStartedGuard) {
                                sIsStarted = false;
                            }
                        }
                    })
                    .create();
            dialog.setCancelable(false);//blocking back key
            dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
            if (!context.getResources().getBoolean(
                    com.android.internal.R.bool.config_sf_slowBlur)) {
                dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
            }
            dialog.show();
        } else {
            beginShutdownSequence(context,false);
        }
    }

其中这个方法是copy shutdown的方法。其中里面的资源换成reboot的资源,其中会发现beginShutdownSequence变成双参了,这就是下一步需要改的

3.将beginShutdownSequence函数变成双参,其中第二参数是boolean类型的,(这个参数的作用是识别是关机还是重启,关机时true,重启是false)

    下面这个只是改过的代码片段,其中只是改一部分就行了。

     if (shutdown) {
                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);
                if (!context.getResources().getBoolean(
                          com.android.internal.R.bool.config_sf_slowBlur)) {
                    pd.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
                }

                 pd.show();
            } else {
                 pd = new ProgressDialog(context);
                 pd.setTitle(context.getText(com.android.internal.R.string.reboot));
                 pd.setMessage(context.getText(com.android.internal.R.string.reboot_progress));
                 pd.setIndeterminate(true);
                 pd.setCancelable(false);
                 pd.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
                 if (!context.getResources().getBoolean(
                          com.android.internal.R.bool.config_sf_slowBlur)) {
                     pd.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
                 }

                 pd.show();
            }

     这个就是当确定后,对reboot和shutdown显示的不一样而已。

4.这一步的话,我们发现,其实我们已经把beginShutdownSequence变成双参了,那么我们需要在调用他的地方也变成双参,其中遵循的原则是reboot是false,而shutdown是true;

5,。将reboot(1,2,3)里面调用shutdown的函数注释掉,而是调用我们第二步的reboot(,true);

6.就是你在上面中所以的资源东西都是要自己给framework中添加的了,这个可不要忘了。

以上就是在系统中添加一个重启功能,其中可能有一些纰漏,望大家积极指正,以上代码在本人机器人测试ok。



其实重启也没那么多的代码,其中大部分代码也是为了显示想关机一样有效果。


你可能感兴趣的:(thread,android,dialog,action,手机,frameworks)