31

Is there a way to receive something like PHONE_UNLOCKED (with a BroadcastReceiver of some kind)?

I have a service running that shows a Toast when the screen is turned on. Unfortunately a few phones don't show it until they're unlocked. Most of the times the Toast message is already gone then.

Naddy
  • 2,664
  • 6
  • 25
  • 38
Saman Miran
  • 424
  • 1
  • 6
  • 14
  • possible duplicate of [Android - detect phone unlock event, not screen on](http://stackoverflow.com/questions/3446202/android-detect-phone-unlock-event-not-screen-on) – Joachim Isaksson Nov 26 '13 at 18:07

1 Answers1

58

There is a Broadcast Receiver Action ACTION_USER_PRESENT here is the implementation of ACTION_USER_PRESENT and ACTION_SHUTDOWN

add this to your application Manifests

<receiver android:name=".UserPresentBroadcastReceiver">
  <intent-filter>
    <action android:name="android.intent.action.USER_PRESENT" />
    <action android:name="android.intent.action.ACTION_SHUTDOWN" />
 </intent-filter>
</receiver>

to receive the actions

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class UserPresentBroadcastReceiver extends BroadcastReceiver {

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

        /*Sent when the user is present after 
         * device wakes up (e.g when the keyguard is gone)
         * */
        if(intent.getAction().equals(Intent.ACTION_USER_PRESENT)){

        }
        /*Device is shutting down. This is broadcast when the device 
         * is being shut down (completely turned off, not sleeping)
         * */
        else if (intent.getAction().equals(Intent.ACTION_SHUTDOWN)) {

        }
    }

}

UPDATE:

As part of the Android 8.0 (API level 26) Background Execution Limits, apps that target the API level 26 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest. see

Chathura Wijesinghe
  • 3,310
  • 3
  • 25
  • 41
  • 2
    What would I need ACTION_SHUTDOWN for? – Saman Miran Nov 26 '13 at 22:16
  • I'm going to try it out tomorrow, it's late in the night here – Saman Miran Nov 26 '13 at 22:19
  • @Chathura Thanks for your answer, but please try to e specific. If there's no need of "Intent.ACTION_SHUTDOWN", please remove those kind of 'dead code'. There are many 'android freshers' who gets confused. By the way, thanks for your answer. – Shirish Herwade May 21 '14 at 13:07
  • 6
    that's why I put the comment. This will help to someone – Chathura Wijesinghe May 21 '14 at 17:50
  • action user present is fired after the system lock is unlocked and home screen appears. but i want to display my screen as a lock screen just after the device is unlocked before device lock screen. please suggest . – Sagar Nayak May 21 '16 at 03:44
  • 8
    It's worth mentioning that you won't receive the broadcast from Android O. See: https://developer.android.com/guide/components/broadcast-exceptions.html – diman Jan 17 '18 at 09:18
  • using this method can we detect user unlock feature means when user unlock the phone i want to notify my app ? – Ahmad Dec 11 '19 at 12:11
  • I tried this but it's not working on Chromebook, is there any other workaround for Chromebook? – Nikhil Apr 02 '21 at 09:08