实现效果是这样的,长按电源键弹出关机对话框,选择关机项将呈现关机动画和音乐直到正常关机完毕,下面说说具体思路及实现代码
找到长按电源键控制代码 /frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
- <span style="font-size:12px;">private final Runnable mPowerLongPress = new Runnable() {
- public void run() {
- // The context isn't read
- if (mLongPressOnPowerBehavior < 0) {
- mLongPressOnPowerBehavior = mContext.getResources().getInteger(
- com.android.internal.R.integer.config_longPressOnPowerBehavior);
- }
- switch (mLongPressOnPowerBehavior) {
- case LONG_PRESS_POWER_NOTHING:
- break;
- case LONG_PRESS_POWER_GLOBAL_ACTIONS:
- mPowerKeyHandled = true;
- performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
- sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
- showGlobalActionsDialog();
- break;
- case LONG_PRESS_POWER_SHUT_OFF:
- mPowerKeyHandled = true;
- performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
- sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
- mWindowManagerFuncs.shutdown();
- break;
- }
- }
- };
- </span>
/frameworks/base/policy/src/com/android/internal/policy/impl/GlobalActions.java
- <span style="font-size:12px;">new SinglePressAction(
- com.android.internal.R.drawable.ic_lock_power_off,
- R.string.global_action_power_off) {
- public void onPress() {
- // shutdown by making sure radio and power are handled accordingly.
- mWindowManagerFuncs.shutdown();
- }
- public boolean onLongPress() {
- mWindowManagerFuncs.rebootSafeMode();
- return true;
- }
- public boolean showDuringKeyguard() {
- return true;
- }
- public boolean showBeforeProvisioning() {
- return true;
- }
- });
- </span>
- <span style="font-size:12px;">synchronized (mEnableAnimatingSync) {
- if(!mEnableAnimating) {
- sInstance.mPowerManager.setBacklightBrightness(PowerManager.BRIGHTNESS_DIM);
- } else {
- if (mShutOffAnimation) {
- Log.e(TAG, "mIBootAnim.isCustBootAnim() is true");
- bootanimCust();
- } else {
- pd = new ProgressDialog(context);
- pd.setTitle(context.getText(com.android.internal.R.string.power_off));
- pd.setMessage(context.getText(com.android.internal.R.string.shutdown_progress));
- pd.setIndeterminate(true);
- pd.setCancelable(false);
- pd.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
- /* To fix video+UI+blur flick issue */
- pd.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
- pd.show();
- }
- sInstance.mHandler.postDelayed(mDelayDim, screenTurnOffTime);
- }
- }</span>
- <span style="font-size:12px;">mShutOffAnimation = true;</span>