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