android5.1添加android长按power键重启功能

当用户长按power键的时候,系统会在PhoneWindowManager中调用


mGlobalActions.showDialog,来显示关机、飞行、重启等界面选项。

而我们需要在GlobalActions.Java中创建一个重启的选项。下面我们从GlobalActions中的createDialog节选一段代码:

  1. mItems = new ArrayList<Action>();  
  2. String[] defaultActions = mContext.getResources().getStringArray(  
  3.         com.android.internal.R.array.config_globalActionsList);//读取配置文件中config_globalActionsList内容,下面详细介绍  
  4.   
  5. ArraySet<String> addedKeys = new ArraySet<String>();  
  6. for (int i = 0; i < defaultActions.length; i++) {  
  7.     String actionKey = defaultActions[i];  
  8.     if (addedKeys.contains(actionKey)) {  
  9.         // If we already have added this, don't add it again.  
  10.         continue;  
  11.     }  
  12.     if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {  
  13.         mItems.add(new PowerAction());  
  14.     } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {  
  15.         mItems.add(mAirplaneModeOn);  
  16.     else if (GLOBAL_ACTION_KEY_REBOOT.equals(actionKey)) {//我们自己添加了一个重启功能,在config文件中找到reboot这一项就添加这个reboot  
  17.         mItems.add(new RebootAction());  
  18.     } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {  
  19.         if (Settings.Global.getInt(mContext.getContentResolver(),  
  20.                 Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {  
  21.             mItems.add(getBugReportAction());  
  22.         }  
  23.     } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {  
  24.         if (mShowSilentToggle) {  
  25.             mItems.add(mSilentModeAction);  
  26.         }  
  27.     } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {  
  28.         if (SystemProperties.getBoolean("fw.power_user_switcher"false)) {  
  29.             addUsersToMenu(mItems);  
  30.         }  
  31.     } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {  
  32.         mItems.add(getSettingsAction());  
  33.     } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {  
  34.         mItems.add(getLockdownAction());  
  35.     } else {  
  36.         Log.e(TAG, "Invalid global action key " + actionKey);  
  37.     }  
  38.     // Add here so we don't add more than one.  
  39.     addedKeys.add(actionKey);  
  40. }  

我们自己还需要新建一个RebootAction,我们看下代码:

  1. private final class RebootAction extends SinglePressAction {  
  2.     private RebootAction() {  
  3.         super(com.android.internal.R.drawable.ic_lock_restart,//界面图片和内容  
  4.                 R.string.global_action_restart);  
  5.     }  
  6.   
  7.     public void onPress() {  
  8.         mWindowManagerFuncs.reboot(true);//长按重启  
  9.     }  
  10.   
  11.     public boolean showDuringKeyguard() {  
  12.         return true;  
  13.     }  
  14.   
  15.     public boolean showBeforeProvisioning() {  
  16.         return true;  
  17.     }  
  18. }  

还有需要在资源文件中添加一些内容:

需要在core/res/res/values-zh-rCN/strings.xml中添加如下:

  1. <string name="global_action_restart">"重新启动"</string>  
  2. <string name="restart_confirm_question">"您要重新启动手机吗?"</string>  
  3. <string name="restart_confirm">"您的手机会重新启动。"</string>  
  4. <string name="restart">"重新启动"</string>  

在core/res/res/values/strings.xml中

  1. <string name="global_action_restart">Restart</string>  
  2. <string name="restart_confirm_question">"Would you like to restart?"</string>  
  3. <string name="restart_confirm">"Your phone will restart."</string>  
  4. <string name="restart">"Restart"</string>  

在core/res/res/values/symbols.xml中

  1. <java-symbol type="string" name="global_action_restart"/>  
  2. <java-symbol type="string" name="restart_confirm_question"/>  
  3. <java-symbol type="string" name="restart_confirm"/>  
  4. <java-symbol type="string" name="restart"/>  
  5. <java-symbol type="drawable" name="ic_lock_restart" />  

还需要添加一个图标的图片

  1. diff --git a/core/res/res/drawable-hdpi/ic_lock_restart.png b/core/res/res/drawable-hdpi/ic_lock_restart.png  
  2.   
  3. diff --git a/core/res/res/drawable-ldpi/ic_lock_restart.png b/core/res/res/drawable-ldpi/ic_lock_restart.png  
  4.   
  5. diff --git a/core/res/res/drawable-mdpi/ic_lock_restart.png b/core/res/res/drawable-mdpi/ic_lock_restart.png  
  6.   
  7. diff --git a/core/res/res/drawable-xhdpi/ic_lock_restart.png b/core/res/res/drawable-xhdpi/ic_lock_restart.png  

最后我们需要在core/res/res/values/config.xml中增加几个选项来打开重启等功能

  1. <string-array translatable="false" name="config_globalActionsList">  
  2.     <item>power</item>  
  3.     <item>bugreport</item>  
  4.     <item>users</item>  
  5.     <item>airplane</item>//飞行模式  
  6.     <item>reboot</item>//新增重启功能  
  7. </string-array>  

而上面再GlobalActions中添加的reboot,需要在WindowManagerPolicy新增接口

  1. public void shutdown(boolean confirm);  
  2. public void rebootSafeMode(boolean confirm);  
  3. public void reboot(boolean confirm);//新增的接口  

并且最总在WindowManagerService中实现:

  1. @Override  
  2. public void reboot(boolean confirm) {  
  3.     Log.d(TAG,"reboot confirm:" + confirm);  
  4.     ShutdownThread.reboot(mContext, null, confirm);  
  5. }  

最后调用ShutdownThread的reboot函数。

  1. public static void reboot(final Context context, String reason, boolean confirm) {  
  2.     mReboot = true;//这个参数最后决定是重启还是关机  
  3.     mRebootSafeMode = false;  
  4.     mRebootReason = reason;  
  5.     shutdownInner(context, confirm);  
  6. }  

下面主要看下ShutdownThread的shutdownInner这个函数,主要是创建弹出框,以及开启线程执行关机或重启。

  1. static void shutdownInner(final Context context, boolean confirm) {  
  2.     synchronized (sIsStartedGuard) {  
  3.         if (sIsStarted) {  
  4.             Log.d(TAG, "Request to shutdown already running, returning.");  
  5.             return;  
  6.         }  
  7.     }  
  8.   
  9.     final int longPressBehavior = context.getResources().getInteger(  
  10.                     com.android.internal.R.integer.config_longPressOnPowerBehavior);  
  11.     final int resourceId = mRebootSafeMode  
  12.             ? com.android.internal.R.string.reboot_safemode_confirm  
  13.             : (longPressBehavior == 2  
  14.                     ? com.android.internal.R.string.shutdown_confirm_question  
  15.                     : com.android.internal.R.string.shutdown_confirm);  
  16.   
  17.     Log.d(TAG, "Notifying thread to start shutdown longPressBehavior=" + longPressBehavior);  
  18.   
  19.     final int resourceIdMsg;  
  20.     final int resourceIdTitle;  
  21.     if(mReboot){//这段是我们添加的,根据不同选项创建不同的界面  
  22.         resourceIdTitle = com.android.internal.R.string.restart;  
  23.         resourceIdMsg = longPressBehavior == 2  
  24.                  ? com.android.internal.R.string.restart_confirm_question  
  25.                  : com.android.internal.R.string.restart_confirm;  
  26.     }  
  27.     else if(mRebootSafeMode){  
  28.         resourceIdTitle = com.android.internal.R.string.reboot_safemode_title;  
  29.         resourceIdMsg = com.android.internal.R.string.reboot_safemode_confirm;  
  30.     }  
  31.     else {  
  32.         resourceIdTitle = com.android.internal.R.string.power_off;  
  33.         resourceIdMsg = longPressBehavior == 2  
  34.                 ? com.android.internal.R.string.shutdown_confirm_question  
  35.                 : com.android.internal.R.string.shutdown_confirm;  
  36.     }  
  37.   
  38.     if (confirm) {//这个参数决定是否需要弹出对话框  
  39.         final CloseDialogReceiver closer = new CloseDialogReceiver(context);  
  40.         if (sConfirmDialog != null) {  
  41.             sConfirmDialog.dismiss();  
  42.         }  
  43.         sConfirmDialog = new AlertDialog.Builder(context)//这边弹出一个对话框也将其进行了修改,主要是显示  
  44.                 .setTitle(resourceIdTitle)  
  45.                 .setMessage(resourceIdMsg)  
  46.                 .setPositiveButton(com.android.internal.R.string.yes, new DialogInterface.OnClickListener() {  
  47.                     public void onClick(DialogInterface dialog, int which) {  
  48.                         beginShutdownSequence(context);//当确认了调用beginShutdownSequence函数,执行关机或重启。  
  49.                     }  
  50.                 })  
  51.                 .setNegativeButton(com.android.internal.R.string.no,  new DialogInterface.OnClickListener() {  
  52.                     public void onClick(DialogInterface dialog, int which) {  
  53.                         //set the mReboot to default value for next time.  
  54.                         mReboot = false;//不确认就直接结束了,不调用beginShutdownSequence  
  55.                     }  
  56.                  })  
  57.                 .create();  
  58.         closer.dialog = sConfirmDialog;  
  59.         sConfirmDialog.setOnDismissListener(closer);  
  60.         sConfirmDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);  
  61.         sConfirmDialog.show();  
  62.     } else {//当不需要确认框弹出,直接调用beginShutdownSequence函数,执行关机或重启。  
  63.         beginShutdownSequence(context);  
  64.     }  
  65. }  

beginShutdownSequence最后会调用sInstance.start();开启线程

因此会到run接口,而run接口最后又会调用rebootOrShutdown,根据reboot是重启还是关机。

  1. public static void rebootOrShutdown(boolean reboot, String reason) {  
  2.     if (reboot) {  
  3.         Log.i(TAG, "Rebooting, reason: " + reason);  
  4.         try {  
  5.             PowerManagerService.lowLevelReboot(reason);//重启  
  6.         } catch (Exception e) {  
  7.             Log.e(TAG, "Reboot failed, will attempt shutdown instead", e);  
  8.         }  
  9.     } else if (SHUTDOWN_VIBRATE_MS > 0) {  
  10.         // vibrate before shutting down  
  11.         Vibrator vibrator = new SystemVibrator();  
  12.         try {  
  13.             vibrator.vibrate(SHUTDOWN_VIBRATE_MS, VIBRATION_ATTRIBUTES);  
  14.         } catch (Exception e) {  
  15.             // Failure to vibrate shouldn't interrupt shutdown.  Just log it.  
  16.             Log.w(TAG, "Failed to vibrate during shutdown.", e);  
  17.         }  
  18.   
  19.         // vibrator is asynchronous so we need to wait to avoid shutting down too soon.  
  20.         try {  
  21.             Thread.sleep(SHUTDOWN_VIBRATE_MS);  
  22.         } catch (InterruptedException unused) {  
  23.         }  
  24.     }  
  25.   
  26.     // Shutdown power  
  27.     Log.i(TAG, "Performing low-level shutdown...");  
  28.     PowerManagerService.lowLevelShutdown();//关机  
  29. }  

这样整个添加重启功能就结束了。

原文地址

http://blog.csdn.net/kc58236582/article/details/45866617


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值