As per official documentation,the BroadcastReceiver for screen lock/unlock events can only be registered dynamically (by calling registerReceiver from within the Activity) starting from Android O (API 26). Registering these events in the AndroidManifest.xml no longer works hence an application is no longer capable of receiving these events (ACTION_SCREEN_ON/ ACTION_USER_PRESENT) when the app is not running (has been previously killed by Android or force closed by user).
Same is also mentioned in the post : Android - detect phone unlock event, not screen on
Is there any other alternate reliable and possible way to get screen lock/unlock event ?
Tried implicit broadcast for receiving screen lock/unlock event by registering ACTION_USER_PRESENT in AndroidManifest .xml file of the app. But as per doc, implcit broadcast apart from some exceptions won't work. You have to do it using dynamic registration by registerReceiver in activity class. So, if the app is killed by Android OS or maybe force stopped by the user, screen lock/unlock broadcast is not received by the app.
Activity class dynamic broadcast receiver
registerReceiver(new PhoneUnlockedReceiver(),
new IntentFilter("android.intent.action.USER_PRESENT"));
public class PhoneUnlockedReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(
Context.KEYGUARD_SERVICE);
if (keyguardManager.isKeyguardSecure()) {
//phone was unlocked, add handling here
}
}
}