9

There seems to be different opinions on whether it is possible to catch the ACTION_USER_PRESENT screen unlock through the manifest.

This thread implies no it can't be done:

Android Broadcast Receiver Not Working

This thread implies yes it can be done:

Broadcast Receiver for ACTION_USER_PRESENT,ACTION_SCREEN_ON,ACTION_BOOT_COMPLETED

I'm not able to get the event working with either a 2.3.3 or 3.2 emulator.

Does anyone else have recent experience with this? And perhaps a code sample to share?

Community
  • 1
  • 1
user1007614
  • 91
  • 1
  • 1
  • 5

2 Answers2

10

Use a receiver:

public class Receive extends BroadcastReceiver {

if (intent.getAction() != null) {
            if
                    ( intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Intent s = new Intent(context, MainActivity.class);
                    s.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                    context.startActivity(s);
}}

And in your manifest:

    <receiver
        android:name=".Receive"
        android:enabled="true"
        android:exported="false">
        <intent-filter>
            <action android:name="android.intent.action.USER_PRESENT"/>
        </intent-filter>
    </receiver>
Prasad
  • 3,462
  • 1
  • 23
  • 28
steo
  • 293
  • 1
  • 4
  • 15
  • i used above your code but first time my lock activity is open but i don't know after that it automaticlly call wallpaper dialog when screen on.why this dialog open instead of my lock activity when screen on...please help quickly thanks... – Nirav Mehta Apr 12 '14 at 10:05
  • 1
    I would not use `android:exported="false"` which is also not allowed at a ``-Tag, see http://stackoverflow.com/questions/29081414/why-wont-this-broadcast-receiver-work-in-lollipop#comment58423396_29274695 – OneWorld Feb 12 '16 at 09:17
2

The official document says:

Beginning with Android 8.0 (API level 26), the system imposes additional restrictions on manifest-declared receivers.

If your app targets Android 8.0 or higher, you cannot use the manifest to declare a receiver for most implicit broadcasts (broadcasts that don't target your app specifically). You can still use a context-registered receiver when the user is actively using your app.

so only some exception can receive implicit, manifest-defined events.

Short answer: so it's not possible anymore to declare it in the manifest. but it's available by context-registering.

Amir Hossein
  • 392
  • 3
  • 11