通过监听android.intent.action.CLOSE_SYSTEM_DIALOGS广播的方式亲测可用!
BroadcastReceiver mHomeKeyEventReceiver = new BroadcastReceiver() {String SYSTEM_REASON = "reason";String SYSTEM_HOME_KEY = "homekey";String SYSTEM_HOME_KEY_LONG = "recentapps";@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {String reason = intent.getStringExtra(SYSTEM_REASON);if (TextUtils.equals(reason, SYSTEM_HOME_KEY)) {//表示按了home键,程序到了后台Toast.makeText(getApplicationContext(), "home", 1).show();}else if(TextUtils.equals(reason, SYSTEM_HOME_KEY_LONG)){//表示长按home键,显示最近使用的程序列表}}}}
在Activity onCreate中添加监听
registerReceiver(mHomeKeyEventReceiver, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
在Activity onDestroy中取消监听
unregisterReceiver(mHomeKeyEventReceiver);
,无论身处何处,只要有一颗放松而美好的心态,生活便是美好!