Android监听HOME按键


class HomeKeyEventBroadCastReceiver extends BroadcastReceiver {
    static final String SYSTEM_REASON = "reason";
    static final String SYSTEM_HOME_KEY = "homekey";// home key
    static final String SYSTEM_RECENT_APPS = "recentapps";// long home key

    @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 (reason != null) {
                if (reason.equals(SYSTEM_HOME_KEY)) {
                    // home key处理点
                    Log.d(TAG, "HOME CLICK!!!!!!!");
                } else if (reason.equals(SYSTEM_RECENT_APPS)) {
                    // long home key处理点
                    Log.d(TAG, "HOME LOND CLICK!!!!!!!");
                }
            }
        }
    }
}

在Activity onCreate中添加:
HomeKeyEventBroadCastReceiver mReceiver = new HomeKeyEventBroadCastReceiver();
registerReceiver(receiver, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));

并在Activity onDestory中添加:
unregisterReceiver(mReceiver);

你可能感兴趣的:(Android监听HOME按键)