有些特殊的应用需要禁掉Home按键操作,但是Android没有提供相应的接口来干这个事,我们需要自己动手修改源代码来达到该目的,并且添加开关量,使得在应用程序中能灵活控制该功能。
Home按键的处理在PhoneWindowManager中,因为它的行为与系统结合紧密,通常短按用来退出到Home界面,长按用来调出任务管理器,而且在某些状态下不能响应,如锁屏等。所以在PhoneWindowManager中实现了interceptKeyBeforeDispatching()方法,该方法在按键事件被发送之前进行处理或截断,以处理系统相应的功能。对HOME键的处理如下:
if (keyCode == KeyEvent.KEYCODE_HOME) {
// If we have released the home key, and didn't do anything else
// while it was pressed, then it is time to go home!
if (!down) {
final boolean homeWasLongPressed = mHomeLongPressed;
mHomePressed = false;
mHomeLongPressed = false;
if (!homeWasLongPressed) {
//禁止短按home键
boolean disableHomeKey = Settings.System.getInt(mContext.getContentResolver(),Settings.System.HOME_KEY_SWITCH,1) == 0? false:true;
if(disableHomeKey) {
return -1;
}
try {
IStatusBarService statusbar = getStatusBarService();
if (statusbar != null) {
statusbar.cancelPreloadRecentApps();
}
} catch (RemoteException e) {
Slog.e(TAG, "RemoteException when showing recent apps", e);
// re-acquire status bar service next time it is needed.
mStatusBarService = null;
}
mHomePressed = false;
if (!canceled) {
// If an incoming call is ringing, HOME is totally disabled.
// (The user is already on the InCallScreen at this point,
// and his ONLY options are to answer or reject the call.)
boolean incomingRinging = false;
try {
if (MSimTelephonyManager.getDefault().isMultiSimEnabled()) {
ITelephonyMSim telephonyMSimService = getMSimTelephonyService();
if (telephonyMSimService != null) {
incomingRinging = telephonyMSimService.isRinging(0)
|| telephonyMSimService.isRinging(1);
}
} else {
ITelephony telephonyService = getTelephonyService();
if (telephonyService != null) {
incomingRinging = telephonyService.isRinging();
}
}
} catch (RemoteException ex) {
Log.w(TAG, "RemoteException from getPhoneInterface()", ex);
}
if (incomingRinging) {
Log.i(TAG, "Ignoring HOME; there's a ringing incoming call.");
} else {
launchHomeFromHotKey();
}
} else {
Log.i(TAG, "Ignoring HOME; event canceled.");
}
return -1;
}
}
// If a system window has focus, then it doesn't make sense
// right now to interact with applications.
WindowManager.LayoutParams attrs = win != null ? win.getAttrs() : null;
if (attrs != null) {
final int type = attrs.type;
if (type == WindowManager.LayoutParams.TYPE_KEYGUARD
|| type == WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG) {
// the "app" is keyguard, so give it the key
return 0;
}
final int typeCount = WINDOW_TYPES_WHERE_HOME_DOESNT_WORK.length;
for (int i=0; i<typeCount; i++) {
if (type == WINDOW_TYPES_WHERE_HOME_DOESNT_WORK[i]) {
// don't do anything, but also don't pass it to the app
return -1;
}
}
}
if (down) {
if (!mHomePressed && mLongPressOnHomeBehavior == LONG_PRESS_HOME_RECENT_SYSTEM_UI) {
try {
IStatusBarService statusbar = getStatusBarService();
if (statusbar != null) {
statusbar.preloadRecentApps();
}
} catch (RemoteException e) {
Slog.e(TAG, "RemoteException when preloading recent apps", e);
// re-acquire status bar service next time it is needed.
mStatusBarService = null;
}
}
if (repeatCount == 0) {
mHomePressed = true;
} else if ((event.getFlags() & KeyEvent.FLAG_LONG_PRESS) != 0) {
if (!keyguardOn) {
//禁止长按Home键
boolean disableHomeKeyLong = Settings.System.getInt(mContext.getContentResolver(),Settings.System.HOME_KEY_LONG_SWITCH,1) == 0? false:true;
if(disableHomeKeyLong) {
return -1;
}
handleLongPressOnHome();
}
}
}
return -1;
}
如上,我们在相应的地方都添加了开关,当然首先我们需要在Settings.System中添加对应的变量,接下来如何控制呢?你需要WRITE_SETTINGS权限,然后就可以开关状态了。
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
Settings.System.putInt(getContentResolver(), Settings.System.HOME_KEY_SWITCH, 1);
Settings.System.putInt(getContentResolver(), Settings.System.HOME_KEY_LONG_SWITCH, 1);