[Monitoring of the Home button]

1. Event listener for Home button

对于Home键的监听不是那么容易,因为Home键可以将程序退出放在后台,所以这个事件是直接分发给系统,系统接收到之后做相应处理,Home键的事件不是直接传递到应用里面.所以在上述监听Back键的代码中,相应的回调中是收不到Home键的事件的.
  • Implementation method one:

    /**
     * 短按Home键灰进入该方法,起到间接监听到home时间
     */
    @Override
    protected void onUserLeaveHint() {
          
                      
        System.exit(0);    
        super.onUserLeaveHint();
    }
    
  • Implementation method two:

    Refer to the blog link at the end of the article. The monitoring of the Home button is mainly realized by registering a broadcast receiver, intercepting the system action that closes the window, and then analyzing whether it is the Home button, the application switching button, or other functions according to the specific parameters in the Intent. button.

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
          
          
        Log.i(LOG_TAG, "onKeyDown: keyCode -- " + keyCode);
    
        switch (keyCode) {
          
          
        case KeyEvent.KEYCODE_BACK:
            Log.i(LOG_TAG, "KeyEvent.KEYCODE_BACK");
            break;
        case KeyEvent.KEYCODE_MENU:
            Log.i(LOG_TAG, "KeyEvent.KEYCODE_MENU");
            break;
        case KeyEvent.KEYCODE_HOME:
            Log.i(LOG_TAG, "KeyEvent.KEYCODE_HOME");
            // 收不到
            break;
        case KeyEvent.KEYCODE_APP_SWITCH:
            Log.i(LOG_TAG, "KeyEvent.KEYCODE_APP_SWITCH");
            // 收不到
            break;
        default:
            break;
        }
        return super.onKeyDown(keyCode, event);
    }
    

2. Broadcast monitoring of the Home button:

It is not so easy to monitor the Home button, because the Home button can exit the program and put it in the background, so this event is directly distributed to the system, and the system will do corresponding processing after receiving it. The event of the Home button is not directly passed to the application. So in In the above code that listens to the Back key, the corresponding callback does not receive the event of the Home key.

对Home键的监听主要通过注册广播接收器实现,拦截让窗口关闭的系统动作,然后根据Intent里面的具体参数,分析当前到底是Home键, 应用切换键,还是其他功能按键.

The receiver is implemented as follows:

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

public class HomeWatcherReceiver extends BroadcastReceiver {
    
    
    private static final String LOG_TAG = "HomeReceiver";
    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";

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

            if (SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)) {
    
    
                // 短按Home键
                Log.i(LOG_TAG, "homekey");

            }
            else if (SYSTEM_DIALOG_REASON_RECENT_APPS.equals(reason)) {
    
    
                // 长按Home键 或者 activity切换键
                Log.i(LOG_TAG, "long press home key or activity switch");

            }
            else if (SYSTEM_DIALOG_REASON_LOCK.equals(reason)) {
    
    
                // 锁屏
                Log.i(LOG_TAG, "lock");
            }
            else if (SYSTEM_DIALOG_REASON_ASSIST.equals(reason)) {
    
    
                // samsung 长按Home键
                Log.i(LOG_TAG, "assist");
            }

        }
    }

}
  • Home key monitor broadcast registration:

There are two ways to register broadcast receivers, one is static registration, which is declared in the manifest; the other is dynamic registration, which is registered in Java code.

The above receiver that monitors the Home key is statically registered as follows:
xml <receiver android:name="com.mengdd.hellohome.HomeWatcherReceiver" > <intent-filter> <action android:name="android.intent.action.CLOSE_SYSTEM_DIALOGS" /> </intent-filter> </receiver>
  But it is found that the static registration does not work, that is, the onReceive callback cannot be received.

采用动态注册:

```java
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);
    }
}

```

Called in Activity's onResume and onPause respectively:

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

    registerHomeKeyReceiver(this);
}

@Override
protected void onPause() {

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

Guess you like

Origin blog.csdn.net/UserFrank/article/details/129205717