Android - how to receive broadcast intents ACTION_SCREEN_ON/OFF?
Alternatively you can use the power manager to detect screen locking.
@Override
protected void onPause()
{
super.onPause();
// If the screen is off then the device has been locked
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
boolean isScreenOn;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
isScreenOn = powerManager.isInteractive();
} else {
isScreenOn = powerManager.isScreenOn();
}
if (!isScreenOn) {
// The screen has been locked
// do stuff...
}
}
I believe that those actions can only be received by receivers registered in Java code (via registerReceiver()
) rather than through receivers registered in the manifest.