android 监听Home键

/**
 * Home 键监听,当按下Home键时,系统会发出action为Intent.ACTION_CLOSE_SYSTEM_DIALOGS的BroadcastReceiver
 * 在程序里动态注册监听,即可达到监听Home键的效果
 */
public class InnerReceiver extends BroadcastReceiver {

    final String SYSTEM_DIALOG_REASON_KEY = "reason";
    final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
    final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";

    @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_DIALOG_REASON_KEY);
            if (reason != null) {
                Log.e("TAB", "action:" + action + ",reason:" + reason);
                if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
                    // 短按home键
                    Log.e("TAB", "home key down....");
                } else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
                    // 长按home键
                    Log.e("TAB", "home long key down...");
                }
            }
        }
    }

}

原文地址:https://www.cnblogs.com/lianghui66/p/3222735.html