For the app I'm writing, if the user locks their phone while running the app I want a pin screen to pop up when the application resumes from the locked state. Is there a callback method or something that I can use to capture that state change?
Asked
Active
Viewed 1,898 times
2 Answers
6
You will need to extend BroadcastReceiver, which has a method called onReceive, which expects a context and an intent.
You can then ask the intent something like:
if(intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
// Do something really cool
}else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
// Do something else equally cool
}
Cheers...

SemperGumbee
- 474
- 1
- 8
- 19
-
The above method works if you want to catch screen off and on and not if device keyguard locked/unlocked(for this check below)... – Navin Ilavarasan Jan 24 '12 at 17:07
-
@Navin `check below` is a bad way to delegate a reader (is invalid if your question got more up votes than this one). You should say that someone should read your answer :) – WarrenFaith Jan 24 '12 at 17:11
-
new to the world of StackOverflow...anyways thanks for bringing it up. – Navin Ilavarasan Jan 24 '12 at 17:17
-
@Navin You are correct sir. I was thinking about just storing a flag in preferences in the if blocks and then checking against them in the onResume() method of the activity, because they will probably unlock the screen in order to get to the activity itself... – SemperGumbee Jan 24 '12 at 17:25
4
Create a broadcast receiver for ACTION_USER_PRESENT ....set a flag in your application preference when the onReceive() of the broadcast receiver is invoked...In the onResume() of your activity check for the flag...If flag is set(means the user has locked and unlocked the phone) show the PIN activity(do not forget to reset the flag in the preference).

Navin Ilavarasan
- 1,271
- 11
- 15