Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register for Intent.ACTION_DREAMING_STARTED in AndroidManifest

So I've been trying to register Intent.ACTION_DREAMING_STARTED inside my AndroidManifest without success.

        <application>
        ...
            <receiver android:name=".broadcastReceivers.DreamingReceiver">
                <intent-filter>
                    <action android:name="android.intent.action.DREAMING_STOPPED"/>
                    <action android:name="android.intent.action.DREAMING_STARTED"/>
                </intent-filter>
            </receiver>
        ...
        </application>

Here's the definition of the receiver.

public class DreamingReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        Log.d("DreamingReceiver", "Intent: " + intent.getAction());
    }
}

When daydream actually starts, there's just no log from that BroadcastReceiver.


However, when I define the receiver in my Activity's onCreate method like so:

        this.receiver = new BroadcastReceiver()
        {
            @Override
            public void onReceive(Context context, Intent intent)
            {
                Log.d("Activity Dreaming", "Intent: " + intent.getAction());
            }
        };

        IntentFilter filter = new IntentFilter(Intent.ACTION_DREAMING_STARTED);
        filter.addAction(Intent.ACTION_DREAMING_STOPPED);

        this.registerReceiver(this.receiver, filter);

I do receive the log whenever my device is entering/leaving daydream. Is there a reason why the BroadcastReceiver that I defined in my AndroidManifest doesn't catch the Intent ?

Apparently, this doesn't need any permission according the the Intent reference

FYI, running on Android KitKat 4.4

like image 732
Snaker Avatar asked Jan 07 '23 13:01

Snaker


1 Answers

Unfortunately the actions DREAMING_STARTED and DREAMING_STOPPED are marked with the flag FLAG_RECEIVER_REGISTERED_ONLY as you can see here:

private final Intent mDreamingStartedIntent = new Intent(Intent.ACTION_DREAMING_STARTED)
        .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
private final Intent mDreamingStoppedIntent = new Intent(Intent.ACTION_DREAMING_STOPPED)
        .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);

Quoting developer.android.com:

public static final int FLAG_RECEIVER_REGISTERED_ONLY
If set, when sending a broadcast only registered receivers will be called -- no BroadcastReceiver components will be launched.

So you can't receive these intent with a BroadcastReceiver declared in the AndroidManifest.xml. Your only option is to register dynamically the BroadcastReceiver.

like image 135
Mattia Maestrini Avatar answered Jan 21 '23 22:01

Mattia Maestrini