Android Home按键监听广播

Android Home按键监听广播

    • 1、Android Home按键监听广播
    • 2、注册

1、Android Home按键监听广播

代码如下:

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

public class HomeReceiver extends BroadcastReceiver {
    public final String SYSTEM_DIALOG_REASON_KEY = "reason";
    public final String HOME_KEY = "homekey";

    @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_DIALOG_REASON_KEY);
            if (HOME_KEY.equals(reason)) {
            }
        }
    }
}

2、注册

注册:

 	homeReceiver = new HomeReceiver();
 	IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
 	registerReceiver(homeReceiver, filter);

取消注册:

        if(homeReceiver!=null){
            unregisterReceiver(homeReceiver);
        }

另外:可能有些设备接收不到。

你可能感兴趣的:(Android)