0

I have an app that needs to detect when the use locks his phone while it's activity is running. Right now the only way I have found is to make a loop with

KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if( myKM.inKeyguardRestrictedInputMode()) {
}

but it really doesn't seem like the right solution and uses up a LOT of cpu. Is there a better way?

Tomhawk
  • 188
  • 8
  • Like a [BroadcastReceiver](https://stackoverflow.com/questions/3170563/android-detect-phone-lock-event)? – codeMagic Oct 24 '17 at 18:48
  • @codeMagic This would not trigger if the screen is already locked when I enable the receiver. – Tomhawk Oct 24 '17 at 19:21
  • Why would it already be locked if the user goes to the screen that has the receiver? – codeMagic Oct 24 '17 at 19:23
  • @codeMagic My activity, when it launches would ask the user to lock the screen. If he already knows the app he might just launch the activity and lock the screen before it finished loading. If he does that and I use a broadcastreceiver it doesn't work. – Tomhawk Oct 24 '17 at 21:21

0 Answers0