Here\'s my scenario. I have an app that is playing backgound sounds. Using the BroadcastReceiver I can tell when the display turns off, and then kill the sounds. I can also
Satur9nine's solution was right at the time, but since then isKeyguardRestricatedInputMode()
was deprecated. Some powerManager related functionalities are now deprecated as well.
There's a newer, more accurate solution: isKeyguardLocked() for whether the device is locked, and a different approach to obtain whether the screen is interactive; You're looking for a combination of both.
KeyguardManager appKeyguard = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
PowerManager appPowerManager = (PowerManager) getSystemService(Context,POWER_SERVICE);
boolean showing = !appKeyguard.isKeyguardLocked() && appPowerManager.isInteractive();
((PowerManager) getSystemService(Context.POWER_SERVICE)).isScreenOn()
((PowerManager) getSystemService(Context.POWER_SERVICE)).isScreenOn()
tells if the screen is on. So, it gets true if the screen is on but the device is locked. Instead,
inKeyguardRestrictedInputMode()
gets true just if the device is locked.
You can try the KeyguardManager to check if the device is locked. Here is some code (I haven't tried this myself):
KeyguardManager kgMgr = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
boolean showing = kgMgr.inKeyguardRestrictedInputMode();
Good luck!