Android 5.1长按电源键添加重启功能

来源:互联网 发布:淘宝微信支付怎么弄 编辑:程序博客网 时间:2024/05/09 08:12
原址:http://blog.csdn.net/zhoumushui

现在长按Power键只有一个关机键,需要添加一个重启,以下是我的添加步骤:



1.在frameworks/base/core/res/res/values/config.xml里添加重启:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <!-- Defines the default set of global actions. Actions may still be disabled or hidden based  
  2.      on the current state of the device.  
  3.      Each item must be one of the following strings:  
  4.      "power" = Power off  
  5.      "settings" = An action to launch settings  
  6.      "airplane" = Airplane mode toggle  
  7.      "bugreport" = Take bug report, if available  
  8.      "silent" = silent mode  
  9.      "users" = list of users  
  10.      -->  
  11. <string-array translatable="false" name="config_globalActionsList">  
  12.     <item>power</item>  
  13.     <item>reboot</item>  
  14.     <item>bugreport</item>  
  15.     <item>users</item>  
  16. </string-array>  

2.添加中英文的重启string:

frameworks/base/core/res/res/values/strings.xml

frameworks/base/core/res/res/values-zh-rCN/strings.xml


3.修改GlobalActions:

frameworks/base/policy/src/com/Android/internal/policy/impl/GlobalActions.Java

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. --- a/frameworks/base/policy/src/com/android/internal/policy/impl/GlobalActions.java  
  2. +++ b/frameworks/base/policy/src/com/android/internal/policy/impl/GlobalActions.java  
  3. @@ -107,6 +107,7 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac  
  4.      private static final String GLOBAL_ACTION_KEY_USERS = "users";  
  5.      private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";  
  6.      private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";  
  7. +       private static final String GLOBAL_ACTION_KEY_REBOOT = "reboot";  
  8.    
  9.      private final Context mContext;  
  10.      private final WindowManagerFuncs mWindowManagerFuncs;  
  11. @@ -306,6 +307,8 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac  
  12.              }  
  13.              if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {  
  14.                  mItems.add(new PowerAction());  
  15. +                       } else if(GLOBAL_ACTION_KEY_REBOOT.equals(actionKey)){  
  16. +                               mItems.add(new RebootAction());  
  17.              } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {  
  18.                  mItems.add(mAirplaneModeOn);  
  19.              } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {  
  20. @@ -371,7 +374,7 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac  
  21.    
  22.          @Override  
  23.          public boolean onLongPress() {  
  24. -            mWindowManagerFuncs.rebootSafeMode(true);  
  25. +            // mWindowManagerFuncs.rebootSafeMode(true);  
  26.              return true;  
  27.          }  
  28.    
  29. @@ -392,6 +395,42 @@ class GlobalActions implements DialogInterface.OnDismissListener, DialogInterfac  
  30.          }  
  31.      }  
  32.    
  33. +  
  34. +    private final class RebootAction extends SinglePressAction implements LongPressAction {  
  35. +        private RebootAction() {  
  36. +            super(com.android.internal.R.drawable.ic_lock_power_off,  
  37. +                R.string.global_action_reboot);  
  38. +        }  
  39. +  
  40. +        @Override  
  41. +        public boolean onLongPress() {  
  42. +            //mWindowManagerFuncs.rebootSafeMode(true);  
  43. +            return true;  
  44. +        }  
  45. +  
  46. +        @Override  
  47. +        public boolean showDuringKeyguard() {  
  48. +            return true;  
  49. +        }  
  50. +  
  51. +        @Override  
  52. +        public boolean showBeforeProvisioning() {  
  53. +            return true;  
  54. +        }  
  55. +  
  56. +        @Override  
  57. +        public void onPress() {  
  58. +                       try {  
  59. +                               Intent intent = new Intent(Intent.ACTION_REBOOT);  
  60. +                               intent.putExtra("nowait"1);  
  61. +                               intent.putExtra("interval"1);  
  62. +                               intent.putExtra("window"0);  
  63. +                               mContext.sendBroadcast(intent);  
  64. +                       } catch (Exception e) {  
  65. +                       }  
  66. +               }  
  67. +    }  
  68. +  
  69.      private Action getBugReportAction() {  
  70.          return new SinglePressAction(com.android.internal.R.drawable.ic_lock_bugreport,  
  71.                  R.string.bugreport_title) {  

可以看到onPress()中是对重启的实现:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. Intent intent = new Intent(Intent.ACTION_REBOOT);  
  2. intent.putExtra("nowait"1);  
  3. intent.putExtra("interval"1);  
  4. intent.putExtra("window"0);  
  5. mContext.sendBroadcast(intent);  

Done!



修改图标:

0 0