监听广播  ACTION_CLOSE_SYSTEM_DIALOGS

    private void registerHomeReceiver(){
        IntentFilter homeFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        registerReceiver(homePressReceiver, homeFilter);
    }
    //HOME键监听
    private BroadcastReceiver homePressReceiver = new BroadcastReceiver() {
        final String SYSTEM_DIALOG_REASON_KEY = "reason";
        final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            String action = intent.getAction();
            if(action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)){
                String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
                System.out.println("reason : " + reason);
                if(reason!=null&&reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)){
                    System.out.println("按HOME键使应用进入了后台");
                }
            }
        }
    };

除按home键外,还可检测长按home键,以及按锁屏事件等

04-26 00:47