长按电源键,为Android手机添加自动重启Item

修改Framwork层源码添加重启效果如下  :  仿照关机代码写重启效果

长按电源键,为Android手机添加自动重启Item

修改实现的方法如下:

1.添加重新启动的图标 分别适应  高清 高 中 低    4 种不同的分辨率 路径如下

frameworks/base/core/res/res/drawable-hdpi/ic_lock_reboot.png

frameworks/base/core/res/res/drawable-ldpi/ic_lock_reboot.png

frameworks/base/core/res/res/drawable-mdpi/ic_lock_reboot.png

/frameworks/base/core/res/res/drawable-xhdpi/ic_lock_reboot.png

图标如下:

长按电源键,为Android手机添加自动重启Item

2.准备一下重启提示语的字符串

支持英语修改路径:frameworks/base/core/res/res/values/strings.xml

 	<string name="reboot_confirm" product="tablet">Your phone will reboot.</string>
 	<string name="reboot_confirm" product="default">Your phone will reboot.</string>
 	<string name="reboot_confirm_question">Do you want to reboot?</string>
 	<string name="reboot">Reboot</string>

支持中文修改路径:

    <string name="reboot">重新启动</string>    
    <string name="reboot_confirm" product="tablet">您的手机将会重新启动.</string>
    <string name="reboot_confirm" product="default">您的手机将会重新启动.</string>    
    <string name="reboot_confirm_question">您要重新启动吗?</string>

3.为弹出的对话框添加一个重启的item

修改路径如下:frameworks/base/policy/src/com/android/internal/policy/impl/GlobalActions.java

 // first: power off  这个是关机的item
        mItems.add(
            new SinglePressAction(
                    com.android.internal.R.drawable.ic_lock_power_off,
                    R.string.global_action_power_off) {

                public void onPress() {
                    // shutdown by making sure radio and power are handled accordingly.
                    mWindowManagerFuncs.shutdown(true);
                }

                public boolean onLongPress() {
                    mWindowManagerFuncs.rebootSafeMode(true);
                    return true;
                }

                public boolean showDuringKeyguard() {
                    return true;
                }

                public boolean showBeforeProvisioning() {
                    return true;
                }
            });
//----------------------添加重启的item  add start 
        mItems.add(
            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.
                    mWindowManagerFuncs.reboot(true);
                }

                public boolean onLongPress() {
                    return true;
                }

                public boolean showDuringKeyguard() {
                    return true;
                }

                public boolean showBeforeProvisioning() {
                    return true;
                }
            });     // add end 


4.添加重启的方法   仿照关机跟进入安全模式的方法写重启方法

修改路径: frameworks/base/core/java/android/view/WindowManagerPolicy.java

        public void shutdown(boolean confirm);
        public void reboot(boolean confirm);                     // add this line  
        public void rebootSafeMode(boolean confirm);
5.实现重启reboot的方法

添加路径如下:frameworks/base/services/java/com/android/server/wm/WindowManagerService.java

  // 关机
    public void shutdown(boolean confirm) {
        ShutdownThread.shutdown(mContext, confirm);
    }
   //add start 添加从其方法
    public void reboot(boolean confirm) {
        ShutdownThread.reboot(mContext,"", confirm);
    }

    // Called by window manager policy.  Not exposed externally.
  //进入安全模式
    public void rebootSafeMode(boolean confirm) {
        ShutdownThread.rebootSafeMode(mContext, confirm);
    }


6.重启方法调用线程实现

修改路径:frameworks/base/services/java/com/android/server/power/ShutdownThread.java

修改ShutdownThread  类中的shutdownInner方法 共3个修改点

 static void shutdownInner(final Context context, boolean confirm) {
        // ensure that only one thread is trying to power down.
        // any additional calls are just returned
        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;
        final int longPressBehavior = context.getResources().getInteger(
                com.android.internal.R.integer.config_longPressOnPowerBehavior);
        int resourceId = mRebootSafeMode                   // -----------modify ------delete  final  
            ? com.android.internal.R.string.reboot_safemode_confirm
            : (longPressBehavior == 2
                    ? com.android.internal.R.string.shutdown_confirm_question
                    : com.android.internal.R.string.shutdown_confirm);

        Log.d(TAG, "Notifying thread to start shutdown longPressBehavior=" + longPressBehavior);
     
		if(mReboot) resourceId = com.android.internal.R.string.reboot_confirm;      //------------add  this line 


        if (confirm) {
            final CloseDialogReceiver closer = new CloseDialogReceiver(context);
            if (sConfirmDialog != null) {
                sConfirmDialog.dismiss();
            }
            if (sConfirmDialog == null) {
                Log.d(TAG, "PowerOff dialog doesn‘t exist. Create it first");
                sConfirmDialog = new AlertDialog.Builder(context)
                    .setTitle(mRebootSafeMode
                            ? com.android.internal.R.string.reboot_safemode_title
                            : (mReboot?com.android.internal.R.string.reboot:com.android.internal.R.string.power_off))//modify this line .添加判断重启
                    .setMessage(resourceId)
                    .setPositiveButton(com.android.internal.R.string.yes, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                            beginShutdownSequence(context);
                            if (sConfirmDialog != null) {
                            sConfirmDialog = null;
                            }
                            }
                            })
                .setNegativeButton(com.android.internal.R.string.no, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                        synchronized (sIsStartedGuard) {
                        sIsStarted = false;
                        }
                        if (sConfirmDialog != null) {
                        sConfirmDialog = null;
                        }
                        }
                        })
                .create();
                sConfirmDialog.setCancelable(false);//blocking back key
                sConfirmDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
                /*if (!context.getResources().getBoolean(
                  com.android.internal.R.bool.config_sf_slowBlur)) {
                  sConfirmDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
                  }*/
                /* To fix video+UI+blur flick issue */
                sConfirmDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
            }

            closer.dialog = sConfirmDialog;
            sConfirmDialog.setOnDismissListener(closer);

            if (!sConfirmDialog.isShowing()) {
                sConfirmDialog.show();
            }
        } else {
            beginShutdownSequence(context);
        }
    }

7.添加新的API接口

修改路径:frameworks/base/api/current.txt

    field public static final int ic_lock_power_off = 17301552; // 0x1080030
    field public static final int ic_lock_reboot = 17301662; // 0x108009e

8.对所用到的资源进行声明 (这个是Android  4.1版本中的)

修改路径:frameworks/base/core/res/res/values/public.xml

  <public type="drawable" name="ic_lock_reboot" id="0x0108009e" />

9.以上修改好后,需要对所用到的资源进行声明

修改路径:frameworks/base/core/res/res/values/symbols.xml

      <java-symbol type="string" name="reboot" />
      <java-symbol type="string" name="reboot_confirm" />
      <java-symbol type="string" name="global_action_reboot" />





长按电源键,为Android手机添加自动重启Item,布布扣,bubuko.com

长按电源键,为Android手机添加自动重启Item

上一篇:移动端轮播图


下一篇:08、移动常见问题