2

I am trying to write a small app that will catch the KeyEvent of the back button been pressed, when the screen is locked.
I saw that you can easily override onKeyDown or onBackPressed in order to catch this event, but this will work only if the activity is running.
As I understand on some of the android phones, if not on all of them, the OS doesn't listen to the back and menu buttons while the screen is locked.

EDIT
I have read couple of posts about capturing the event of the user pressing the volume buttons, and all says their isn't a way to do so when the screen is off.
Is their a reason to belive the case is change regarding the back button?

Community
  • 1
  • 1
Presen
  • 1,809
  • 4
  • 31
  • 46

1 Answers1

3

1)Register with BroadCast Receiver for Intent.ACTION_SCREEN_OFF and Intent.ACTION_SCREEN_ON

2) Check if Screen is locked using Keyguard Manager like :

KeyguardManager kManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
boolean isLocked = kManager.inKeyguardRestrictedInputMode();

or without using BroadCast Receiver, you can check like :

KeyguardManager kManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
if( kManager .inKeyguardRestrictedInputMode()) {
 //locked
} else {
 //not locked
 }

Next Check for back Button Press event if screen is locked.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
   //Back Button is pressed
}
return super.onKeyDown(keyCode, event);
}
N Kaushik
  • 2,198
  • 18
  • 29