zoukankan      html  css  js  c++  java
  • android4.2添加重启菜单项

    本文主要是针对android4.2关机菜单添加重启功能




    A.关机提示
    android4.2/frameworks/base/policy/src/com/android/internal/policy/impl/GlobalActions.java

     

    [java] view plaincopy
    1. private void handleShow() {  
    2.      // 本函数不需要修改...  
    3.      awakenIfNecessary();  
    4.      mDialog = createDialog();  
    5.      prepareDialog();  
    6.      WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();  
    7.      attrs.setTitle("GlobalActions");  
    8.      mDialog.getWindow().setAttributes(attrs);  
    9.      mDialog.show();// 关机选择对话框  
    10.   
    11.      mDialog.getWindow().getDecorView().setSystemUiVisibility(View.STATUS_BAR_DISABLE_EXPAND);  
    12.   
    13.      /**************** 
    14.  
    15.       如果只需要关机确认可以修改为: 
    16.       awakenIfNecessary(); 
    17.       mWindowManagerFuncs.shutdown(true); 
    18.  
    19.      *******************/  
    20.  }  

       关机选项菜单 找到createDialog() 赋值power off那一项,  修改关机为:         mWindowManagerFuncs.reboot(true);
    [java] view plaincopy
    1. private GlobalActionsDialog createDialog() {  
    2.             ....// 省略  
    3.             // first: power off  
    4.             mItems.add(  
    5.                 ....// 省略  
    6.                });  
    7. /////////////////////////////////////////////////////////////////////////  
    8.             // second:reboot  添加这一项    
    9.             try {// 添加try catch 为了避免错误...  
    10.                mItems.add(  
    11.                    new SinglePressAction(  
    12.                            com.android.internal.R.drawable.ic_lock_power_off,  
    13.                            R.string.global_action_reboot) {  
    14.   
    15.                        public void onPress() {  
    16.                            // shutdown by making sure radio and power are handled accordingly.  
    17.             //                    mWindowManagerFuncs.rebootSafeMode(true);  
    18.                        //    mWindowManagerFuncs.reboot(mContext,null,false);  
    19.                        //    ShutdownThread.reboot(mContext,null,false);  
    20.                            try {  
    21.                                mWindowManagerFuncs.reboot(true);  
    22.                            } catch (Exception e) {  
    23.                                // TODO: handle exception  如果有问题, 最好在这里打印一些信息看看是不是这里出问题了  
    24.                            }  
    25.   
    26.                        }  
    27.   
    28.                        public boolean showDuringKeyguard() {  
    29.                            return true;  
    30.                        }  
    31.   
    32.                        public boolean showBeforeProvisioning() {  
    33.                            return true;  
    34.                        }  
    35.                            });  
    36.                         } catch (Exception e) {  
    37.                            // TODO: handle exception    
    38.                         }  
    39.                     }  
    40.                     <pre name="code" class="java">              /////////////////////////////////////////////////////////////////////////  

     ........//省略
    
    B. 1)    android4.2/frameworks/base/core/java/android/view/WindowManagerPolicy.java    找到WindowManagerFuncs这个interface的定义,增加如下function的声明:
    
    [java] view plaincopy
    1. public interface WindowManagerFuncs {  
    2.   
    3.     public void reboot(boolean confirm);  
    4.   
    5. }  
     
        2)
        android4.2/frameworks/base/services/java/com/android/server/wm/WindowManagerService.java

        找到shutdown()函数,在其后面添加reboot()函数,函数内容如下:

    [java] view plaincopy
    1. // Called by window manager policy. Not exposed externally.  
    2.   
    3. @Override  
    4.   
    5. public void reboot(boolean confirm) {  
    6.   
    7.     ShutdownThread.reboot(mContext, confirm);  
    8.   
    9. }  


    C. 修改关机时的进度框提示

          android4.2/frameworks/base/services/java/com/android/server/power/ShutdownThread.java

     
    [java] view plaincopy
    1. /*/ 
    2.  //修改前: 
    3.  …… 
    4.   pd.setTitle(context.getText(com.android.internal.R.string.power_off)); 
    5.   pd.setMessage(context.getText(com.android.internal.R.string.shutdown_progress)); 
    6.  …… 
    7.  /*/  
    8.  //修改后:  
    9.  ……  
    10.  if(mReboot){     // reboot progress  
    11.           pd.setTitle(context.getText(com.android.internal.R.string.global_action_reboot));  
    12.           pd.setMessage(context.getText(com.android.internal.R.string.reboot_progress));  
    13.       }else{          // shutdown progress  
    14.           pd.setTitle(context.getText(com.android.internal.R.string.power_off));  
    15.           pd.setMessage(context.getText(com.android.internal.R.string.shutdown_progress));  
    16.       }  
    17.  ……  
    18.  //*/  


    D.  重启询问对话框   // 这部分可能代码不一样  不过都是修改shutdownInner函数里面的sConfirmDialog这个dialog的

     
    1. shutdownInner(final Context context, boolean confirm)//   
    2.       if(Settings.Global.getInt(context.getContentResolver(), Settings.Global.DEVICE_PROVISIONED, 1)==1 &&  
    3.           SystemProperties.getBoolean("ro.sys.bootfast"false)){  
    4.               ......  
    5.               // 这部分可能代码不一样  不过都是修改sConfirmDialog这个dialog的  
    6.               if(mReboot){  
    7.                   idBootMode = com.android.internal.R.string.reboot_confirm_question;  
    8.               }else{  
    9.                   idBootMode = com.android.internal.R.string.shutdown_confirm_question;  
    10.               }  
    11.               sConfirmDialog = new AlertDialog.Builder(context)  
    12.                   .setTitle(mReboot  
    13.                       ? com.android.internal.R.string.global_action_reboot  
    14.                       : com.android.internal.R.string.power_off)  
    15.                   .setMessage(idBootMode)  
    16.                   .setPositiveButton(com.android.internal.R.string.yes, new DialogInterface.OnClickListener() {  
    17.                   public void onClick(DialogInterface dialog, int which) {  
    18.                       if(mPolicy!=null)  
    19.                           mPolicy.acquireBAView();  
    20.                       beginShutdownSequence(context);  
    21.                   }  
    22.                   })  
    23.                   .setNegativeButton(com.android.internal.R.string.no, null)  
    24.                   .create();  
    25.   
    26.              }  
        /////////////////////说明  无关  只做注释///////////////////
                  // 下面这个函数会杀死所有的activity, 桌面不能杀死"com.android.launcher", 否则就会导致关机的时候显示开机动画
                  private  void killRemoveActivity(Context context)

         /////////////////////end说明///////////////////       

        

    F. 重启功能实现了,但是不能到此结束,定制Android系统的重启功能,添加选项和修改重启提示框时加入了“重启”和“正在重启”的字符串,
       所以会涉及到在系统资源文件中添加新的字符串,
       源码中资源文件涉及到的多国语言直接忽略,我只在values/strings.xml和values-zh-rCN/strings.xml两个文件中加入对应的字符串:

        路径: framework/base/core/res/res/valuse/   or  framework/base/core/res/res/values-zh-rCN/

      上面是拷贝别人的,  实际上我发现如果不在symbols.xml添加相关的字符串信息,  编译会报错的,  故应该在framework/base/core/res/res/valuse/ symbols.xml参考其他的把自己的添加上:

    [html] view plaincopy
    1. strings.xml:  
    2.      <string name="reboot_confirm_question">"Do you want to reboot?"</string>  
    3.      <string name="global_action_reboot">"Reboot"</string>  
    4.      <string name="reboot_progress">"Rebooting..."</string>  
    5.   
    6.  symbols.xml  
    7.      <java-symbol type="string" name="reboot_confirm_question" />  
    8.      <java-symbol type="string" name="global_action_reboot" />  
    9.      <java-symbol type="string" name="reboot_progress" />  


    E.最后编译
    因为修改涉及到系统资源文件又涉及到policy.jar包,经过多次尝试,正确的编译顺序如下:
    步骤1.编译frameworks/base/res,在out/target/product/X设备名X/system/framework/目录下生成framework-res.apk
    步骤2.编译frameworks/base/,在o同样目录下生成framework.jar包(不能忽略,不然步骤3编译报错)
    步骤3.编译frameworks/base/services/java/     生成services.jar

    步骤4.编译frameworks/base/policy,在同样目录下生成android.policy.jar 包



    参考: 

    http://blog.csdn.net/zzp16/article/details/7829063

    http://www.2cto.com/kf/201403/288710.html

  • 相关阅读:
    建立文件结构
    PCL类的设计结构
    如何编写新的PCL类
    PCL推荐的命名规范(2)
    PCL推荐的命名规范(1)
    PCL中异常处理机制
    如何增加新的PointT类型
    hdoj 1728 逃离迷宫
    ny710 外星人的供给站
    ny714 Card Trick
  • 原文地址:https://www.cnblogs.com/liang123/p/6325385.html
Copyright © 2011-2022 走看看