Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ACTION_USER_PRESENT in manifest with BroadcastReceiver

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?

like image 988
user1007614 Avatar asked Feb 03 '23 12:02

user1007614


2 Answers

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>
like image 173
steo Avatar answered Mar 28 '23 17:03

steo


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.

like image 24
amirhossein p Avatar answered Mar 28 '23 18:03

amirhossein p