Application Callback On Device Unlock

2k Views Asked by At

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?

2

There are 2 best solutions below

4
On BEST ANSWER

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...

0
On

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).