美文网首页Android Utils
Android 监听 Home 键

Android 监听 Home 键

作者: 几千里也 | 来源:发表于2017-02-09 13:54 被阅读49次
        private static final String SYSTEM_DIALOG_REASON_KEY = "reason";
        private static final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
        private static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
        private static final String SYSTEM_DIALOG_REASON_LOCK = "lock";
        private static final String SYSTEM_DIALOG_REASON_ASSIST = "assist";
        private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                Log.i(TAG, "onReceive: action: " + action);
    
                if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
                    // android.intent.action.CLOSE_SYSTEM_DIALOGS == action
                    String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
    
                    if (SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)) {
                        // 短按 Home 键
                        Log.i(TAG, "reason: homekey");
                    } else if (SYSTEM_DIALOG_REASON_RECENT_APPS.equals(reason)) {
                        // 长按 Home 键或者 Recent 键
                        Log.i(TAG, "reason: recentapps");
                    } else if (SYSTEM_DIALOG_REASON_LOCK.equals(reason)) {
                        // 锁屏
                        Log.i(TAG, "reason: lock");
                    } else if (SYSTEM_DIALOG_REASON_ASSIST.equals(reason)) {
                        // Samsung 长按 Home 键
                        Log.i(TAG, "reason: assist");
                    } else {
                        Log.i(TAG, "reason: " + reason);
                    }
                }
            }
        };
    
        private IntentFilter mIntentFilter;
    
    
            mIntentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
            registerReceiver(mBroadcastReceiver, mIntentFilter);
    
            if (null != mBroadcastReceiver) {
                unregisterReceiver(mBroadcastReceiver);
            }
    
    
    
    

    相关文章

      网友评论

        本文标题:Android 监听 Home 键

        本文链接:https://www.haomeiwen.com/subject/byiouttx.html