1. 程式人生 > >android 監聽Home鍵和亮滅屏

android 監聽Home鍵和亮滅屏

1.註冊廣播

        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);//home鍵
        //filter.addAction(Intent.ACTION_USER_PRESENT);//使用者解鎖廣播
        filter.addAction(Intent.ACTION_SCREEN_OFF);//滅屏廣播,亮屏為on
        registerReceiver(mReceiver, filter);

2.接受廣播

    private static final String SYSTEM_DIALOG_REASON_KEY = "reason";
    private static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
    BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context arg0, Intent intent) {
            String action = intent.getAction();
            if
(Util.EXIT_ACTION.equals(action)){ mTabHost.setCurrentTab(0); mRadio_mylock.setChecked(true); }else if(Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)){ String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY); if (SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)) { // 短按Home鍵
} }else if(Intent.ACTION_SCREEN_OFF.equals(action)){ //滅屏 } } };