設置Android設備長按Power直接關機
Android4.0.3版本中長按Power按鍵跳出關機選項對話框;但在一些項目會需要長按Power直接關機而不是跳出關機選項對話框。Android4.0.3源碼中並無長按直接關機的代碼及選項,因此需要我們自己添加。
思路及實現:
參照長按Power按鍵跳出的關機對話框及關機流程部分代碼,可以發現涉及到長按Power按鍵,關機及需要實現我們需要的功能的文件集中在:
framework/base/core/res/res/values/config.xml
framework/base/policy/src/com/android/internal/policy/impl/PhoneWindowManger.java
具體修改如下:
第一步:
在PhoneWindowManager.java中
添加static final int LONG_PRESS_POWER_OFF_NOW = 3;
如下:
[java]
static final int LONG_PRESS_POWER_NOTHING = 0;
static final int LONG_PRESS_POWER_GLOBAL_ACTIONS = 1;
static final int LONG_PRESS_POWER_SHUT_OFF = 2;
static final int LONG_PRESS_POWER_OFF_NOW = 3;
其中各個變量即為長按Power按鍵時的條件選項
然後找到mPowerLongPress ,這是個Runnable,當長按Power案件之後系統啟動的線程
具體添加長按關機代碼如下:
[java]
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);
ShutdownThread.shutdown(mContext, true);
break;
case <span style="color:#cc0000;">LONG_PRESS_POWER_OFF_NOW</span>://長按Power按鍵關機代碼
Log.d(TAG,"-->> mPowerLongPress:run():LONG_PRESS_POWER_OFF_NOW");
mPowerKeyHandled = true;
performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
sendCloseSystemWindows(SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS);
ShutdownThread.shutdown(mContext, false);
break;
}
}
第二步:
在第一步中我們添加了長按Power按鍵之後關機的處理代碼,接下來需要在config.xml文件中修改默認設置,修改為默認長按直接關機。找到<integer name="config_longPressOnPowerBehavior">1</integer>
將默認的1修改為3,如下:
[html]
<!-- <integer name="config_longPressOnPowerBehavior">1</integer> -->
<integer name="config_longPressOnPowerBehavior">3</integer>