Android 监听手机home键

今天说说在程序里面怎么监听手机home键和home长按

1.  

registerReceiver(mHomeKeyEventReceiver, new IntentFilter(
        Intent.ACTION_CLOSE_SYSTEM_DIALOGS));

2.

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键,程序到了后台
                Toast.makeText(context, "home", Toast.LENGTH_SHORT).show();
            }else if(TextUtils.equals(reason, SYSTEM_HOME_KEY_LONG)){
                //表示长按home键,显示最近使用的程序列表
                Toast.makeText(context, "管理器", Toast.LENGTH_SHORT).show();
            }
        }
    }
};

猜你喜欢

转载自blog.csdn.net/qq_36488374/article/details/80137462