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));