实验几次之后(android4.1 和android4.2)发现在单个的activity里面有以下几个方法可以使用:
方法1:onSaveInstanceState方法
下面这个方法可以处理home的监听问题。
Java代码
protected void onSaveInstanceState(Bundle outState)
但这个方法不是很好,不推荐
方法2:onUserLeaveHint方法
Java代码
@Overrideprotected voidonUserLeaveHint() {
Log.d("aeon","onUserLeaveHint");super.onUserLeaveHint();
}
这个方法会在onSaveInstanceState之前执行,根据api的解释,这个方法还比较合适的。
方法3:ACTION_CLOSE_SYSTEM_DIALOGS
在使用广播监听方面可以使用ACTION_CLOSE_SYSTEM_DIALOGS
Java代码
//注册ReceiverHomeKeyEventBroadCastReceiver receiver= newHomeKeyEventBroadCastReceiver();
registerReceiver(receiver,newIntentFilter(
Intent. ACTION_CLOSE_SYSTEM_DIALOGS));
方法4:framework PhoneWindowManager.java 处理
想要完全监听home键需要在framework层去处理。
/frameworks/base/policy/src/com/android/internal/policy/impl/PhoneWindowManager.java
里面去修改private void handleLongPressOnHome() 这个方法。
Java代码
private voidhandleLongPressOnHome() {//We can't initialize this in init() since the configuration hasn't been loaded yet.
if (mLongPressOnHomeBehavior < 0) {
mLongPressOnHomeBehavior=mContext.getResources().getInteger(R.integer.config_longPressOnHomeBehavior);if (mLongPressOnHomeBehavior < LONG_PRESS_HOME_NOTHING ||mLongPressOnHomeBehavior>LONG_PRESS_HOME_RECENT_SYSTEM_UI) {
mLongPressOnHomeBehavior=LONG_PRESS_HOME_NOTHING;
}
}if (mLongPressOnHomeBehavior !=LONG_PRESS_HOME_NOTHING) {
performHapticFeedbackLw(null, HapticFeedbackConstants.LONG_PRESS, false);
sendCloseSystemWindows(SYSTEM_DIALOG_REASON_RECENT_APPS);//Eat the longpress so it won't dismiss the recent apps dialog when//the user lets go of the home key
mHomeLongPressed = true;
}if (mLongPressOnHomeBehavior ==LONG_PRESS_HOME_RECENT_DIALOG) {
showOrHideRecentAppsDialog(RECENT_APPS_BEHAVIOR_SHOW_OR_DISMISS);
}else if (mLongPressOnHomeBehavior ==LONG_PRESS_HOME_RECENT_SYSTEM_UI) {try{
IStatusBarService statusbar=getStatusBarService();if (statusbar != null) {
statusbar.toggleRecentApps();
}
}catch(RemoteException e) {
Slog.e(TAG,"RemoteException when showing recent apps", e);//re-acquire status bar service next time it is needed.
mStatusBarService = null;
}
}
}
只要把handleLongPressOnHome里面做出相对应的处理就ok