首页 > 系统 > Android > 正文

Android捕获home和recent app按键

2019-11-09 13:53:30
字体:
来源:转载
供稿:网友

Android开发中,有时候需要捕获home和recent app按键,进行相应处理。可以通过注册如下广播接收器:

PRivate BroadcastReceiver mHomeKeyEventReceiver = new BroadcastReceiver() {        String SYSTEM_REASON = "reason";        String SYSTEM_HOME_KEY = "homekey";        String SYSTEM_RECENT_APPS = "recentapps";                 @Override          public void onReceive(Context context, Intent intent) {            String action = intent.getAction();            if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {            Log.d(TAG, "ACTION_CLOSE_SYSTEM_DIALOGS");                String reason = intent.getStringExtra(SYSTEM_REASON);                Log.d(TAG, "reason - " + reason);                if (TextUtils.equals(reason, SYSTEM_HOME_KEY)) {                     // Home key is pressed                                    }else if( TextUtils.equals(reason, SYSTEM_RECENT_APPS) ){                    // Recent apps key.                 }            }        }    };其中,intent中的extra信息SYSTEM_REASON,表示的即是按下的按键,SYSTEM_HOME_KEY表示按下的事HOME键,SYSTEM_RECENT_APPS表示按下的是recent app键。


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表