Search code examples
androidbroadcastreceiverandroid-8.0-oreoandroid-implicit-intent

Android implicit BroadcastReceiver with signature permission is not called in Android O


In my first application, I define a custom permission and an implicit BroadcastReceiver in manifest file:

<permission
        android:name="com.example.test.TEST"
        android:protectionLevel="signature" />

<receiver
        android:name=".TestBroadcastReceiver"
        android:enabled="true"
        android:exported="true"
        android:permission="com.example.test.TEST">
        <intent-filter>
                <action android:name="com.example.test.TEST_RECEIVER" />
        </intent-filter>
</receiver>

And this is the TestBroadcastReceiver.java:

public class TestBroadcastReceiver extends BroadcastReceiver {

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

        Log.d("Test", "Hello World!");
        Toast.makeText(context, "Hello World!", Toast.LENGTH_LONG).show();

    }
}

In my second app, I've added the permission in manifest file:

<uses-permission android:name="com.example.test.TEST" />

And here, I send the broadcast:

getActivity().sendBroadcast(new Intent("com.example.test.TEST_RECEIVER"));

But nothing is called in first app. I know we can't use implicit broadcast in android O and above but according to here, there is an exception for broadcasts that require a signature permission:

Broadcasts that require a signature permission are exempted from this restriction, since these broadcasts are only sent to apps that are signed with the same certificate, not to all the apps on the device.

So how can I signal my other apps in android O?


Solution

  • According to CommonsWare answer, the problem is that I was missing setPackage() part. So I changed the code as below and now broadcast is received:

    getActivity().sendBroadcast(new Intent("com.example.test.TEST_RECEIVER").setPackage("com.example.test"));