android監聽手機屏幕狀態

private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (!TextUtils.isEmpty(action)) {
                switch (action) {
                    case Intent.ACTION_SCREEN_OFF:
                        L.e("屏幕關閉,變黑");
//                        Activity activity = AppManager.getAppManager().currentActivity();
//                        AppManager.getAppManager().finishActivity(activity);

                        break;
                    case Intent.ACTION_SCREEN_ON:
                        L.e( "屏幕開啓,變亮");
                        break;
                    case Intent.ACTION_USER_PRESENT:
                        L.e( "解鎖成功");

//                        boolean passwordInActivity = AppManager.isForeground(getApplicationContext(), "PasswordInActivity");

//                        L.e( "解鎖成功==========="+passwordInActivity);
                        

                        break;
                    default:
                        break;
                }
            }
        }
    };

 

在方法中調用:
registerReceiver(mBroadcastReceiver, new IntentFilter(Intent.ACTION_SCREEN_OFF));
registerReceiver(mBroadcastReceiver, new IntentFilter(Intent.ACTION_SCREEN_ON));
registerReceiver(mBroadcastReceiver, new IntentFilter(Intent.ACTION_USER_PRESENT));
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章