0

I want an app to send a broadcast to a several other apps. And I am not managing to make it work :-(

In the app sending the broadcast I simply do:

        sendBroadcast((new Intent("myBusiness.intent.action.MY_ACTION"))
            .putExtra("some_extra_data", "the_extra_data"), "my_receiver_permission");

In the apps that are supposed to receive the broadcast I use context-registered receivers to minimize system load but it is not working... The apps are targeting SDK 26 and I've tested it on several versions... I declare a member variable in the MainActivity class to hold the receiver being registered:

public class MainActivity extends AppCompatActivity {
...
    public static BroadcastReceiver mMyReceiver = null;
...
}

I register and unregister the receiver in the onCreate() and onDestroy() methods:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        if (savedInstanceState == null) {
            ...
            this.registerReceiver(mMyReceiver = new MyReceiver(),
            new IntentFilter("myBusiness.intent.action.MY_ACTION"),
            "my_receiver_permission", null);
            ...
        }
    }

    @Override
    protected void onDestroy() {
        ...
        if (mMyReceiver != null)
            this.unregisterReceiver(mMyReceiver);
    }

And have declared the receiver class:

public class MyReceiver extends BroadcastReceiver {
    public MyReceiver() {
        super();
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // And here is where I would like to do my stuff...
    }
}

As I am using permissions I think that the order of apps installation may be important. So what I am doing is that I am first installing, and executing and closing, the "receiving" apps. Then I install and execute the "broadcasting" app, and close it. And finally I execute one of the "receiving" apps expecting the Broacastreceiver.onReceive() to trigger... but no...

Following Roey's comment I incorporated the receiver in the Manifest and sent the Broadcast using the FLAG_INCLUDE_STOPPED_PACKAGES flag. What made it work if I don't use permissions to protect the receiver. But it still doesn't work if I use permissions :-( This is how the Manifest looks like:

<manifest ...>
...
    <uses-permission android:name="my_receiver_permission" />
    <permission android:name="my_receiver_permission"/>
...
    <application ...>
...
        <receiver
            android:name=".MyReceiver"
            android:enabled="true"
            android:exported="true"
            android:permission="my_receiver_permission">
            <intent-filter>
                <action android:name="myBusiness.intent.action.MY_ACTION" />
            </intent-filter>
        </receiver>
...
     </application>
</manifest>

Any idea why it is not working?? Thaaank yoouuuu

Victor
  • 33
  • 6
  • which android version you are using for broadcast – Ranjan Sep 19 '17 at 18:17
  • Be sure the exported feature for that broadcast enabled. Otherwise the broadcast that in that app not as global. You may find helpful the following link: https://stackoverflow.com/questions/33492790/how-to-send-broadcast-from-one-app-to-another-app – Vurgun M Sep 19 '17 at 18:48
  • Hi Ranjan, thank you for your question! I am broadcasting with a API 23 phone... – Victor Sep 20 '17 at 11:16
  • Hi Vurgun, thank you for your suggestion. I was initially not declaring at all the receiver in the Manifest as I thought (wrongly) that it was not necessary when registering the receiver via context. I have now declared the receiver in the Manifest and edited my initial question to show this. With this it works if I don't use permissions but does not work if I use permissions... – Victor Sep 20 '17 at 11:20

2 Answers2

0

Good way to avoid a broadcast registration issue is by declare the intent in the manifest itself, in APP B :

 <receiver android:name=".BroadcastReceiverExample">
    <intent-filter>
        <action android:name="myBusiness.intent.action.MY_ACTION" />
    </intent-filter>
</receiver>

Also, in order to be sure that APP B is able to get the broadcast is by adding the flag by using the below code before send the message:intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES); In this way APP B will receive the broadcast even if it's not waiting in the background.

  • Thank you very much Roey! It worked! I had tried including the receiver in the Manifest and it didn't work, so I guess the flag made the trick!! – Victor Sep 19 '17 at 20:46
  • Thank you very much Roey! It works if I don't include a permission! I had tried including the receiver in the Manifest and it didn't work, so I guess the flag made the trick!! Any idea on how to make it work with a permission? I have included a android:permission="my_receiver_permission" in the tag and declared the permission in a tag... – Victor Sep 19 '17 at 21:05
  • Hi, I was glad to help. – Android and Security Guru Sep 20 '17 at 09:07
  • I read the following from Android's API guide for Broadcasts (https://developer.android.com/guide/components/broadcasts.html): "Note: If your app targets API level 26 or higher, you cannot use the manifest to declare a receiver for implicit broadcasts (broadcasts that do not target your app specifically), except for a few implicit broadcasts that are exempted from that restriction. In most cases, you can use scheduled jobs instead." – Victor Sep 20 '17 at 14:18
0

Finally found what the issue was with the permissions problem: the permission had to be declared ALSO in the Manifest of the app sending the broadcast...

HOWEVER, it is now working for all APIs up to 25, but not with API 26 :-(

Victor
  • 33
  • 6