Android framework修改,长按power键直接关机,不给客户提示(基于Android10.0.0-r41)
如下图,正常情况下,长按关机键,都会弹出一个对话框,让用户选择Power off关机等操作,我们修改去掉这个弹框,直接长按关机。
frameworks/base/services/core/java/com/android/server/policy/PhoneWindowManager.java
找到powerLongPress方法,注释以下代码。
private void powerLongPress() {
final int behavior = getResolvedLongPressOnPowerBehavior();
// switch (behavior) {
// case LONG_PRESS_POWER_NOTHING:
// break;
// case LONG_PRESS_POWER_GLOBAL_ACTIONS:
// mPowerKeyHandled = true;
// performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, false,
// "Power - Long Press - Global Actions");
// // showGlobalActionsInternal();
// break;
// case LONG_PRESS_POWER_SHUT_OFF:
// case LONG_PRESS_POWER_SHUT_OFF_NO_CONFIRM:
// mPowerKeyHandled = true;
// performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, false,
// "Power - Long Press - Shut Off");
// sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
// mWindowManagerFuncs.shutdown(behavior == LONG_PRESS_POWER_SHUT_OFF);
// break;
// case LONG_PRESS_POWER_GO_TO_VOICE_ASSIST:
// mPowerKeyHandled = true;
// performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, false,
// "Power - Long Press - Go To Voice Assist");
// // Some devices allow the voice assistant intent during setup (and use that intent
// // to launch something else, like Settings). So we explicitly allow that via the
// // config_allowStartActivityForLongPressOnPowerInSetup resource in config.xml.
// launchVoiceAssist(mAllowStartActivityForLongPressOnPowerDuringSetup);
// break;
// case LONG_PRESS_POWER_ASSISTANT:
// mPowerKeyHandled = true;
// performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, false,
// "Power - Long Press - Go To Assistant");
// final int powerKeyDeviceId = Integer.MIN_VALUE;
// launchAssistAction(null, powerKeyDeviceId);
// break;
// }
//新增代码
mPowerKeyHandled = true;
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, false, null);
sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
mWindowManagerFuncs.shutdown(false);
}
mPowerKeyHandled = true;
这行代码设置了一个标记,表示电源键的事件已被处理。通过将其值设置为true
,系统可以知道该操作已经得到了关注,会阻止此事件的默认处理流程或防止多次处理同一事件。
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS, false, null);
这行代码触发了设备的触觉反馈(例如震动),模拟长按的效果。HapticFeedbackConstants.LONG_PRESS
指定了反馈类型为长按,false
参数意味着这个反馈不需要明确的视图上下文(某些情况下反馈效果需要和特定视图关联,但这里不需要),null
则是额外的运动事件参数,这里没有使用。
sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
此函数发送一个请求以关闭所有系统窗口。SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS
是一个常量,说明关闭系统窗口的原因是全局动作被触发。全局动作通常指的是那些从任何界面都能访问的操作,如电源菜单(包含重启、关机等选项)。
mWindowManagerFuncs.shutdown(false);
最后这行代码调用窗口管理器的功能接口来执行关机操作,其中false
//这里false代表不显示关机提示框,true未显示关机提示框,通常这意味着执行一个直接的关机指令,而不是显示一个确认对话框给用户。