本文主要是针对android4.2关机菜单添加重启功能
android4.2/frameworks/base/policy/src/com/android/internal/policy/impl/GlobalActions.java
- private void handleShow() {
- // 本函数不需要修改...
- awakenIfNecessary();
- mDialog = createDialog();
- prepareDialog();
- WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
- attrs.setTitle("GlobalActions");
- mDialog.getWindow().setAttributes(attrs);
- mDialog.show();// 关机选择对话框
- mDialog.getWindow().getDecorView().setSystemUiVisibility(View.STATUS_BAR_DISABLE_EXPAND);
- /****************
- 如果只需要关机确认可以修改为:
- awakenIfNecessary();
- mWindowManagerFuncs.shutdown(true);
- *******************/
- }
关机选项菜单 找到createDialog() 赋值power off那一项, 修改关机为: mWindowManagerFuncs.reboot(true);
- private GlobalActionsDialog createDialog() {
- ....// 省略
- // first: power off
- mItems.add(
- ....// 省略
- });
- /////////////////////////////////////////////////////////////////////////
- // second:reboot 添加这一项
- try {// 添加try catch 为了避免错误...
- mItems.add(
- new SinglePressAction(
- com.android.internal.R.drawable.ic_lock_power_off,
- R.string.global_action_reboot) {
- public void onPress() {
- // shutdown by making sure radio and power are handled accordingly.
- // mWindowManagerFuncs.rebootSafeMode(true);
- // mWindowManagerFuncs.reboot(mContext,null,false);
- // ShutdownThread.reboot(mContext,null,false);
- try {
- mWindowManagerFuncs.reboot(true);
- } catch (Exception e) {
- // TODO: handle exception 如果有问题, 最好在这里打印一些信息看看是不是这里出问题了
- }
- }
- public boolean showDuringKeyguard() {
- return true;
- }
- public boolean showBeforeProvisioning() {
- return true;
- }
- });
- } catch (Exception e) {
- // TODO: handle exception
- }
- }
- <pre name="code" class="java"> /////////////////////////////////////////////////////////////////////////
........//省略 B. 1) android4.2/frameworks/base/core/java/android/view/WindowManagerPolicy.java 找到WindowManagerFuncs这个interface的定义,增加如下function的声明:
- public interface WindowManagerFuncs {
- public void reboot(boolean confirm);
- }
2)
android4.2/frameworks/base/services/java/com/android/server/wm/WindowManagerService.java
找到shutdown()函数,在其后面添加reboot()函数,函数内容如下:
- // Called by window manager policy. Not exposed externally.
- @Override
- public void reboot(boolean confirm) {
- ShutdownThread.reboot(mContext, confirm);
- }
C. 修改关机时的进度框提示
android4.2/frameworks/base/services/java/com/android/server/power/ShutdownThread.java
- /*/
- //修改前:
- ……
- pd.setTitle(context.getText(com.android.internal.R.string.power_off));
- pd.setMessage(context.getText(com.android.internal.R.string.shutdown_progress));
- ……
- /*/
- //修改后:
- ……
- if(mReboot){ // reboot progress
- pd.setTitle(context.getText(com.android.internal.R.string.global_action_reboot));
- pd.setMessage(context.getText(com.android.internal.R.string.reboot_progress));
- }else{ // shutdown progress
- pd.setTitle(context.getText(com.android.internal.R.string.power_off));
- pd.setMessage(context.getText(com.android.internal.R.string.shutdown_progress));
- }
- ……
- //*/
D. 重启询问对话框 // 这部分可能代码不一样 不过都是修改shutdownInner函数里面的sConfirmDialog这个dialog的
- shutdownInner(final Context context, boolean confirm)//
- if(Settings.Global.getInt(context.getContentResolver(), Settings.Global.DEVICE_PROVISIONED, 1)==1 &&
- SystemProperties.getBoolean("ro.sys.bootfast", false)){
- ......
- // 这部分可能代码不一样 不过都是修改sConfirmDialog这个dialog的
- if(mReboot){
- idBootMode = com.android.internal.R.string.reboot_confirm_question;
- }else{
- idBootMode = com.android.internal.R.string.shutdown_confirm_question;
- }
- sConfirmDialog = new AlertDialog.Builder(context)
- .setTitle(mReboot
- ? com.android.internal.R.string.global_action_reboot
- : com.android.internal.R.string.power_off)
- .setMessage(idBootMode)
- .setPositiveButton(com.android.internal.R.string.yes, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int which) {
- if(mPolicy!=null)
- mPolicy.acquireBAView();
- beginShutdownSequence(context);
- }
- })
- .setNegativeButton(com.android.internal.R.string.no, null)
- .create();
- }
// 下面这个函数会杀死所有的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参考其他的把自己的添加上:
- strings.xml:
- <string name="reboot_confirm_question">"Do you want to reboot?"</string>
- <string name="global_action_reboot">"Reboot"</string>
- <string name="reboot_progress">"Rebooting..."</string>
- symbols.xml
- <java-symbol type="string" name="reboot_confirm_question" />
- <java-symbol type="string" name="global_action_reboot" />
- <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