Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - detect phone unlock event, not screen on

Tags:

android

Is there a way to detect when a user unlocks the phone? I know about ACTION_SCREEN_ON and ACTION_SCREEN_OFF, but these seem to be fired when the screen switches on/off when pressing the power button, but not actually when the phone gets unlocked when pressing the Menu button...

I am trying to detect the unlock/lock while an activity is running, and I want to resume the activity once unlocked.

like image 894
Chris Avatar asked Aug 10 '10 05:08

Chris


People also ask

Which tool can bypass Android screen lock and get location history?

iMyFone LockWiper. Highly praised by some of the biggest names like XDAdevelopers, iMyFone LockWiper supports over 6,000 Android devices, including running on 10.0 versions. It can remove pin lock, pattern, password, fingerprint, face ID, and Google account verification.

How do I unlock my phone with tapping the screen?

Double-Tap to Lock and Unlock Android This feature can also be used to lock your phone—double-tap to lock, double-tap to unlock. Alternatively, if double-tapping your screen to lock and unlock your Android still isn't working, you can use a third-party app like Screen Off.

How can I unlock my Android phone automatically?

In the Smart Lock settings menu, tap Trusted Places, then tap Home. Tap Turn on this location and you'll be asked to choose a "Home" address if you haven't already set one up. Set up other places to keep your phone unlocked by tapping Add trusted place.

How do you unlock a locked Android screen?

How to remove Screen Lock on your Android phone. Tap Settings > Security > Screen Lock. If prompted, enter your current lock screen code > None > Delete.


2 Answers

Here's what to do:

Say you want to detect the unlock event and do something in your activity when the phone is unlocked. Have a Broadcast Receiver for ACTION_SCREEN_ON, ACTION_SCREEN_OFF and ACTION_USER_PRESENT.

onResume of the activity will be called when ACTION_SCREEN_ON is fired. Create a handler and wait for ACTION_USER_PRESENT. When it is fired, implement what you want for your activity.

Credit goes to CommonsWare's answer here: Android -- What happens when device is unlocked?

like image 159
Chris Avatar answered Oct 12 '22 15:10

Chris


After struggling with this for a while, I've found that the best way to do this is to register a BroadcastReceiver on the "android.intent.action.USER_PRESENT" action.

"Broadcast Action: Sent when the user is present after device wakes up (e.g when the key-guard is gone)."

To distinguish between cases where the user has turned on phone screen when it wasn't locked, and when they actually unlocked it use the KeyguardManager to check the security settings.

Code example:

Add this to your activity:

registerReceiver(new PhoneUnlockedReceiver(), new IntentFilter("android.intent.action.USER_PRESENT"));

Then use this class:

public class PhoneUnlockedReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        KeyguardManager keyguardManager = (KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE);
        if (keyguardManager.isKeyguardSecure()) {
            //phone was unlocked, do stuff here
        }
    }
}       
like image 25
Doron Manor Avatar answered Oct 12 '22 15:10

Doron Manor