Android 系统修改长按关机键功能

系统环境

瑞星微 px30 android 8.1系统

要实现的功能

原本长按power键的功能是弹出一个选择对话框,关机和重启功能,长按两个选择项会提示是否要进入安全模式,现将它改成长按恢复出厂功能

实现思路

  • power按键和普通的按键一样都是在framework的PhoneWindowManager中的interceptKeyBeforeQueueing方法监听
 @Override
public int interceptKeyBeforeQueueing(KeyEvent event, int policyFlags) {
    if (!mSystemBooted) {
        // If we have not yet booted, don't let key events do anything.
        return 0;
    }
    ....
    ....
     // Handle special keys.
    switch (keyCode) {
         case KeyEvent.KEYCODE_BACK: {
             if (down) {
                 interceptBackKeyDown();
             } else {
                 boolean handled = interceptBackKeyUp(event);

                 // Don't pass back press to app if we've already handled it via long press
                 if (handled) {
                     result &= ~ACTION_PASS_TO_USER;
                 }
             }
             break;
         }
         case KeyEvent.KEYCODE_POWER: {
                // Any activity on the power button stops the accessibility shortcut
                cancelPendingAccessibilityShortcutAction();
                result &= ~ACTION_PASS_TO_USER;
                isWakeKey = false; // wake-up will be handled separately
                if (down) {
                //这里走的是这个方法
                    interceptPowerKeyDown(event, interactive);
                } else {
                    interceptPowerKeyUp(event, interactive, canceled);
                }
                break;
            }
       .... 
	}
}
  • 在interceptKeyBeforeQueueing找到KEYCODE_POWER的代码,这里走的是interceptPowerKeyDown方法
这里分发出去了一个消息MSG_POWER_LONG_PRESS
private void interceptPowerKeyDown(KeyEvent event, boolean interactive) {
	...
	...
	// If the power key has still not yet been handled, then detect short
        // press, long press, or multi press and decide what to do.
        mPowerKeyHandled = hungUp || mScreenshotChordVolumeDownKeyTriggered
                || mA11yShortcutChordVolumeUpKeyTriggered || gesturedServiceIntercepted;
        if (!mPowerKeyHandled) {
            if (interactive) {
                // When interactive, we're already awake.
                // Wait for a long press or for the button to be released to decide what to do.
                if (hasLongPressOnPowerBehavior()) {
                    Message msg = mHandler.obtainMessage(MSG_POWER_LONG_PRESS);
                    msg.setAsynchronous(true);
                    mHandler.sendMessageDelayed(msg,
                            ViewConfiguration.get(mContext).getDeviceGlobalActionKeyTimeout());
                }
            } else {
                wakeUpFromPowerKey(event.getDownTime());

                if (mSupportLongPressPowerWhenNonInteractive && hasLongPressOnPowerBehavior()) {
                    Message msg = mHandler.obtainMessage(MSG_POWER_LONG_PRESS);
                    msg.setAsynchronous(true);
                    mHandler.sendMessageDelayed(msg,
                            ViewConfiguration.get(mContext).getDeviceGlobalActionKeyTimeout());
                    mBeganFromNonInteractive = true;
                } else {
                    final int maxCount = getMaxMultiPressPowerCount();

                    if (maxCount <= 1) {
                        mPowerKeyHandled = true;
                    } else {
                        mBeganFromNonInteractive = true;
                    }
                }
            }
        }        
}
  • 这里分发出去了一个消息MSG_POWER_LONG_PRESS这个消息里面就是一个方法powerLongPress
 case MSG_POWER_LONG_PRESS:
      powerLongPress();
      break;
 //这里是将原本的方法改成恢复出厂设置
 private void powerLongPress() {
        Log.e("zhuw","-----------------powerLongPress----------------");
        if (ActivityManager.isUserAMonkey()) {
            return;
        }
        Intent intent = new Intent(Intent.ACTION_FACTORY_RESET);
        intent.setPackage("android");
        intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
        intent.putExtra(Intent.EXTRA_REASON, "MasterClearConfirm");
        intent.putExtra(Intent.EXTRA_WIPE_EXTERNAL_STORAGE, true);
        intent.putExtra(Intent.EXTRA_WIPE_ESIMS, true);
        mContext.sendBroadcast(intent);
        // Intent handling is asynchronous -- assume it will happen soon.

        /*final int behavior = getResolvedLongPressOnPowerBehavior();
        switch (behavior) {
        case LONG_PRESS_POWER_NOTHING:
            break;
        case LONG_PRESS_POWER_GLOBAL_ACTIONS:
            mPowerKeyHandled = true;
            performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
            showGlobalActionsInternal();
            break;
        case LONG_PRESS_POWER_SHUT_OFF:
        case LONG_PRESS_POWER_SHUT_OFF_NO_CONFIRM:
            mPowerKeyHandled = true;
            performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
            sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
            mWindowManagerFuncs.shutdown(behavior == LONG_PRESS_POWER_SHUT_OFF);
            break;
        }*/
    }
  • 这里原本的被注释的代码是根据config_longPressOnPowerBehavior这个系统属性来决定走哪一步操作 我这里原本的系统属性是1
    系统中搜索属性值
  • 因此对应的LONG_PRESS_POWER_GLOBAL_ACTIONS 原本的关机重启选择框就是在showGlobalActionsInternalshowDialog中实现
void showGlobalActionsInternal() {
        sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
        if (mGlobalActions == null) {
            mGlobalActions = new GlobalActions(mContext, mWindowManagerFuncs);
        }
        final boolean keyguardShowing = isKeyguardShowingAndNotOccluded();
        mGlobalActions.showDialog(keyguardShowing, isDeviceProvisioned());
        if (keyguardShowing) {
            // since it took two seconds of long press to bring this up,
            // poke the wake lock so they have some time to see the dialog.
            mPowerManager.userActivity(SystemClock.uptimeMillis(), false);
        }
    }
public void showDialog(boolean keyguardShowing, boolean deviceProvisioned) {
        if (DEBUG) Slog.d(TAG, "showDialog " + keyguardShowing + " " + deviceProvisioned);
        mKeyguardShowing = keyguardShowing;
        mDeviceProvisioned = deviceProvisioned;
        mShowing = true;
        boolean isTvProduct = SystemProperties.get("ro.target.product","unknown").equals("atv") ||
                           SystemProperties.get("ro.target.product","unknown").equals("box");
        if (mStatusBarConnected && !isTvProduct) {
            mStatusBarInternal.showGlobalActions();
            mHandler.postDelayed(mShowTimeout, 5000);
        } else {
            // SysUI isn't alive, show legacy menu.
            ensureLegacyCreated();
            mLegacyGlobalActions.showDialog(mKeyguardShowing, mDeviceProvisioned);
        }
    }
  • showDialog方法这里有两种UI实现,一个是在SystemUI中,一个是当SystemUI不存在时framework中的旧菜单中实现 两者方法实现差不多,这里主要介绍下显示弹框的配置,它是获取config_globalActionsList数组配置来进行显示 我这里将原来的power改成reset
 <string-array name="config_globalActionsList" translatable="false">
        <!--<item>power</item>-->
        <item>reset</item>
        <item>restart</item>
        <item>bugreport</item>
        <item>users</item>
    </string-array>
  • 代码提供出来,可以参考
    关机选择框添加恢复出厂设置功能

写在最后

  • 本文主要介绍了在Android 8.1 系统中长按power键弹出一个关机菜单的流程并做对应修改
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值