LouisWang:http://blog.csdn.net/louiswangbing
转载请注明出处:http://blog.csdn.net/louiswangbing/article/details/6688240
上一篇文章中讲到android源码定制要点,说了个大概的方法和方向,现在,就来实战一下。
在Android系统中,长按Power键默认会弹出对话框让你选择“飞行模式”,“静音”,“关机”等功能。如下图所示:
但这些功能都对Android-x86和其他终端产品就没什么必要了。本文就简单介绍下如何定制关机界面。
我的目标是长按Power键,将会关机,弹出“设备将要关机”选择对话框。如果可以选择“是”关机,和“否”返回系统。
按照android源码定制要点中提到的,首先你要对整个系统有全面的了解,找到弹出原来这个选择框的代码,它在这里:
frameworks\policies\base\phone\com\android\internal\policy\impl\PhoneWindowManager.java
显示对话框调用的代码如下:
- Java code
Runnable mPowerLongPress = new Runnable() { public void run() { mShouldTurnOffOnKeyUp = false; performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false); sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS); showGlobalActionsDialog(); } };
调用showGlobalActionsDialog方法之后将会聚到有“飞行模式”、“静音”、“关机”等选项的对话框。
找到这里,我们就知道该做什么了!干掉它,换成我们想要的关机代码,就大功告成了!既然这样,事不宜迟,让我们赶快到showGloabalActionDialog方法中看看关机的部分在哪里!
showGlobalActionsDialog的实现部分在这里:
view plain
frameworks\policies\base\phone\com\android\internal\policy\impl\GlobalAction.java
我们进去看看:
- Java code
public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned) { mKeyguardShowing = keyguardShowing; mDeviceProvisioned = isDeviceProvisioned; if (mDialog == null) { mStatusBar = (StatusBarManager)mContext.getSystemService(Context.STATUS_BAR_SERVICE); mDialog = createDialog(); } prepareDialog(); mStatusBar.disable(StatusBarManager.DISABLE_EXPAND); mDialog.show(); }
我们可以很清楚的看到,这里新建了一个mDialog,然后prepare接着就show了它,那么,这个mDialog就是关键了,看看它是怎么被createDialog创建出来的吧,仍然在这个文件中:
- Java code
/** * Create the global actions dialog. * @return A new dialog. */ private AlertDialog createDialog() { mSilentModeToggle = new ToggleAction( R.drawable.ic_lock_silent_mode, R.drawable.ic_lock_silent_mode_off, R.string.global_action_toggle_silent_mode, R.string.global_action_silent_mode_on_status, R.string.global_action_silent_mode_off_status) { void willCreate() { // XXX: FIXME: switch to ic_lock_vibrate_mode when available mEnabledIconResId = (Settings.System.getInt(mContext.getContentResolver(), Settings.System.VIBRATE_IN_SILENT, 1) == 1) ? R.drawable.ic_lock_silent_mode_vibrate : R.drawable.ic_lock_silent_mode; } void onToggle(boolean on) { if (on) { mAudioManager.setRingerMode((Settings.System.getInt(mContext.getContentResolver(), Settings.System.VIBRATE_IN_SILENT, 1) == 1) ? AudioManager.RINGER_MODE_VIBRATE : AudioManager.RINGER_MODE_SILENT); } else { mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL); } } public boolean showDuringKeyguard() { return true; } public boolean showBeforeProvisioning() { return false; } }; mAirplaneModeOn = new ToggleAction( R.drawable.ic_lock_airplane_mode, R.drawable.ic_lock_airplane_mode_off, R.string.global_actions_toggle_airplane_mode, R.string.global_actions_airplane_mode_on_status, R.string.global_actions_airplane_mode_off_status) { void onToggle(boolean on) { if (Boolean.parseBoolean( SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) { mIsWaitingForEcmExit = true; // Launch ECM exit dialog Intent ecmDialogIntent = new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null); ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); mContext.startActivity(ecmDialogIntent); } else { changeAirplaneModeSystemSetting(on); } } @Override protected void changeStateFromPress(boolean buttonOn) { // In ECM mode airplane state cannot be changed if (!(Boolean.parseBoolean( SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) { mState = buttonOn ? State.TurningOn : State.TurningOff; mAirplaneState = mState; } } public boolean showDuringKeyguard() { return true; } public boolean showBeforeProvisioning() { return false; } }; mItems = Lists.newArrayList( // silent mode mSilentModeToggle, // next: airplane mode mAirplaneModeOn, // last: power off new SinglePressAction( com.android.internal.R.drawable.ic_lock_power_off, R.string.global_action_power_off) { [color=#0000FF]public void onPress() { // shutdown by making sure radio and power are handled accordingly. ShutdownThread.shutdown(mContext, true); } [/color] public boolean showDuringKeyguard() { return true; } public boolean showBeforeProvisioning() { return true; } }); mAdapter = new MyAdapter(); final AlertDialog.Builder ab = new AlertDialog.Builder(mContext); ab.setAdapter(mAdapter, this) .setInverseBackgroundForced(true) .setTitle(R.string.global_actions); final AlertDialog dialog = ab.create(); dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG); if (!mContext.getResources().getBoolean( com.android.internal.R.bool.config_sf_slowBlur)) { dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND); } dialog.setOnDismissListener(this); return dialog; }