滅屏(電源) 、亮屏、解鎖、HOME廣播

protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.button_layuout);  

    final IntentFilter filter = new IntentFilter();  
    // 屏幕滅屏廣播  
    filter.addAction(Intent.ACTION_SCREEN_OFF);  
    // 屏幕亮屏廣播  
    filter.addAction(Intent.ACTION_SCREEN_ON);  
    // 屏幕解鎖廣播  
    filter.addAction(Intent.ACTION_USER_PRESENT);  
    // 當長按電源鍵彈出“關機”對話或者鎖屏時系統會發出這個廣播  
    // example:有時候會用到系統對話框,權限可能很高,會覆蓋在鎖屏界面或者“關機”對話框之上,  
    // 所以監聽這個廣播,當收到時就隱藏自己的對話,如點擊pad右下角部分彈出的對話框   
    filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);  

    BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {  

        final String SYSTEM_DIALOG_REASON_KEY = "reason";
        //按下Home鍵
        final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
        //長按Home鍵
        final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";

        @Override  
        public void onReceive(final Context context, final Intent intent) {  
            Log.d(TAG, "onReceive");  
            String action = intent.getAction();  

            if (Intent.ACTION_SCREEN_ON.equals(action)) {  
                Log.d(TAG, "screen on");  
            } else if (Intent.ACTION_SCREEN_OFF.equals(action)) {  
                Log.d(TAG, "screen off");  
            } else if (Intent.ACTION_USER_PRESENT.equals(action)) {  
                Log.d(TAG, "screen unlock");  
            } else if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(intent.getAction())) {  
                Log.i(TAG, " receive Intent.ACTION_CLOSE_SYSTEM_DIALOGS");  

                 String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
                if (reason != null && mOnHomeKeyListener != null) {
                    if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
                        mOnHomeKeyListener.onHomeKeyPressed();
                    } else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
                        mOnHomeKeyListener.onHomeKeyLongPressed();
                    }
                }

            }  
        }  
    };  
    Log.d(TAG, "registerReceiver");  
    registerReceiver(mBatInfoReceiver, filter);  
}  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章