我已经有一个活动,该活动在用户按下解锁按钮之后以及解锁屏幕之前出现。但只会显示一次:在应用启动后,用户首次解锁手机。
要使其再次显示,我必须重新启动该应用程序。我想让它在应用程序运行时(在屏幕上或在后台)每次用户解锁手机时都显示出来
Main Activity
protected void onResume() {
super.onResume();
registerBroadcastReceiver();
}
private void registerBroadcastReceiver() {
final IntentFilter theFilter = new IntentFilter();
theFilter.addAction(Intent.ACTION_SCREEN_ON);
theFilter.addAction(Intent.ACTION_USER_PRESENT);
theFilter.addAction(Intent.ACTION_SCREEN_OFF);
BroadcastReceiver screenOnOffReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String strAction = intent.getAction();
KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if (strAction.equals(Intent.ACTION_SCREEN_OFF) || strAction.equals(Intent.ACTION_USER_PRESENT) || strAction.equals(Intent.ACTION_SCREEN_ON)) {
if (myKM.inKeyguardRestrictedInputMode()) {
System.out.println("Screen off " + "LOCKED");
if (AdActivity.getInstance() == null) {
startActivity(new Intent(MainActivity.this, AdActivity.class));
}
} else {
if (AdActivity.getInstance() == null) {
System.out.println("Screen off " + "UNLOCKED");
startActivity(new Intent(MainActivity.this, AdActivity.class));
}
}
}
}
};
getApplicationContext().registerReceiver(screenOnOffReceiver, theFilter);
}