Android 監聽手機按下Home鍵
阿新 • • 發佈:2018-11-28
系統本身有這個廣播,只需要監聽這個廣播就可以了!
private BroadcastReceiver mHomeKeyEventReceiver = new BroadcastReceiver() { String SYSTEM_REASON = "reason"; String SYSTEM_HOME_KEY = "homekey"; String SYSTEM_HOME_KEY_LONG = "recentapps"; @Override public 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鍵,程式到了後臺// showShortToast("按了home鍵,程式到了後臺"); } } } };
//註冊廣播 registerReceiver(mHomeKeyEventReceiver, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
這樣,在手機按下Home鍵時,可以做想要的操作。