Search code examples
androidandroid-intentbroadcastreceiverandroid-8.0-oreo

Android O : Background execution not allowed: Custom broadcast with permission


In my application I have registered a broadcast receiver for an implicit broadcast by another application protected with permission :

<receiver
    android:name=".receiver.MyReceiver"
    android:exported="true"
    android:permission="owner.custom.permission">
    <intent-filter>
        <action android:name="owner.custom.broadcast"/>
    </intent-filter>
</receiver>

In MyReceiver#onReceive() I am invoking a JobIntentService MyService using enqueueWork():

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, final Intent intent) {
        Log.i(TAG, "Received the broadcast");
        MyService.enqueueWork(context, getServiceIntent(context));
    }

    private Intent getServiceIntent(final Context context) {
        final Intent intent = new Intent(context, MyService.class);
        intent.putExtra("someKey", true);
        return intent;
    }
}

I have the following method in MyService :

public static void enqueueWork(final Context context, final Intent work) {
        enqueueWork(context, MyService.class, 111, work);
    }

Now whenever owner.custom.broadcast is broadcast, MyReceiver is not triggered and I can see the following logs :

07-23 03:56:29.755  3335  3361 W BroadcastQueue: Background execution not allowed: receiving Intent { act=owner.custom.broadcast flg=0x10 } to com.amazon.myApp/.receiver.MyReceiver

Now the thing is I am listening to another such 3rd party implicit broadcast with a different broadcast receiver and invoking MyService over there and it works fine. I am also listening for BOOT_COMPLETED broadcast in a yet another broadcast receiver and invoking MyService over there and it works fine there too.
What are the possible causes for this error which would help me identify if I'm missing something.

UPDATE :
I am now just trying to get the broadcast receiver to trigger but I am still getting the same error. I am trying with nothing but a log line in the receiver :

public class MyReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, final Intent intent) {
        Log.i(TAG, "Received the broadcast");
    }
}

Solution

  • I'm not sure why this solution worked (maybe someone else can elaborate on why) but I was able to get my broadcast receiver to trigger by declaring the permission in my Manifest itself and and also using the same. Find the code changes below :

    <permission
        android:name="owner.custom.permission"
        android:protectionLevel="signatureOrSystem">
    </permission>
    .
    .
    .
    <uses-permission android:name="owner.custom.permission" />