3

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?

Community
  • 1
  • 1
Misagh Emamverdi
  • 3,654
  • 5
  • 33
  • 57
  • 1
    Since this is all in your own app, use an explicit `Intent`. Also note that you need to guarantee that your first app is *always* installed before the second app for a `signature` permission to be effective. – CommonsWare Apr 30 '19 at 13:27
  • @CommonsWare, I replaced the intent with `new Intent("com.example.test.TestBroadcastReceiver").setPackage("com.example.test")`, But problem still exists. – Misagh Emamverdi Apr 30 '19 at 13:43
  • 1
    Your revised `Intent` action does not match the action string in your ``. Either use your original `Intent` action plus the `setPackage("com.example.test")` part, or use `new Intent(context, TestBroadcastReceiver.class)` (if `TestBroadcastReceiver` is in the same app as the broadcast sender). – CommonsWare Apr 30 '19 at 13:45

1 Answers1

1

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"));
Misagh Emamverdi
  • 3,654
  • 5
  • 33
  • 57