Android Home button to listen

For the Home key listener is not so easy, because the Home key to exit the program in the background, so this event is distributed directly to the system, the system act accordingly after receiving, the Home key event is not passed directly to the application inside. So Back key codes in the monitor, the corresponding callback is not received event of the Home key.

Blog links after the reference to the text, listen for the Home key, mainly through registration broadcast receiver implementation, interception let the window closed system actions, and then depending on the parameters of Intent inside, the analysis of the current in the end is the Home key, application switching key, or other functions The key
receiver is implemented as follows:

package com.sinosoft.huataiejia.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.sinosoft.huataiejia.A;
import com.sinosoft.huataiejia.activity.ShowActivity;

/**
 * Author by linkaikai
 * Date on 2019/5/29
 */
public class HomeWatcherReceiver extends BroadcastReceiver {
    private static final String LOG_TAG = "HomeReceiver";

    private static final String SYSTEM_DIALOG_REASON_KEY = "reason";
    //action内的某些reason
    private static final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";//home键旁边的最近程序列表键
    private static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";//按下home键
    private static final String SYSTEM_DIALOG_REASON_LOCK = "lock";//锁屏键
    private static final String SYSTEM_DIALOG_REASON_ASSIST = "assist";//某些三星手机的程序列表键

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
//        App app = (App) context.getApplicationContext();
        Log.i(LOG_TAG, "onReceive: action: " + action);
        if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {//Action
            // android.intent.action.CLOSE_SYSTEM_DIALOGS
            String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
            LogUtils.i(LOG_TAG, "reason: " + reason);

            if (SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)) { // 短按Home键
                //可以在这里实现关闭程序操作。。。
                LogUtils.i(LOG_TAG, "homekey");
         
            } else if (SYSTEM_DIALOG_REASON_RECENT_APPS.equals(reason)) {//Home键旁边的显示最近的程序的按钮
                // 长按Home键 或者 activity切换键
                LogUtils.i(LOG_TAG, "long press home key or activity switch");
            } else if (SYSTEM_DIALOG_REASON_LOCK.equals(reason)) {  // 锁屏,似乎是没有反应,监听Intent.ACTION_SCREEN_OFF这个Action才有用
                LogUtils.i(LOG_TAG, "lock");
            } else if (SYSTEM_DIALOG_REASON_ASSIST.equals(reason)) {   // samsung 长按Home键
                LogUtils.i(LOG_TAG, "assist");
            }

        }
    }

}

Home key monitor of dynamic registration (Android 8.0 phone registered to take static onReceive not receive callbacks)

private static HomeWatcherReceiver mHomeKeyReceiver = null;

    private static void registerHomeKeyReceiver(Context context) {
        Log.i(LOG_TAG, "registerHomeKeyReceiver");
        mHomeKeyReceiver = new HomeWatcherReceiver();
        final IntentFilter homeFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);

        context.registerReceiver(mHomeKeyReceiver, homeFilter);
    }

    private static void unregisterHomeKeyReceiver(Context context) {
        Log.i(LOG_TAG, "unregisterHomeKeyReceiver");
        if (null != mHomeKeyReceiver) {
            context.unregisterReceiver(mHomeKeyReceiver);
        }
    }

Activity of onResume were called in and inside onPause

@Override
    protected void onResume() {
        super.onResume();

        registerHomeKeyReceiver(this);
    }

    @Override
    protected void onPause() {

        unregisterHomeKeyReceiver(this);
        super.onPause();
    }

Reproduced in: https: //www.jianshu.com/p/54b62416a29b

Guess you like

Origin blog.csdn.net/weixin_33724059/article/details/91303885