How to Send Broadcast from One App to Another App

Send broadcast from one Android app to another

There are basically two ways afaik:

  • Broadcast Receiver and using the sendBroadcast method on the sender side

or by using Intents:

  • you can use startActivity(Intent) even with another app, but this will bring the app to foreground rather than doing a job in background.

Use intents if the calling app should dissappear and the called app should be in foreground and use broadcasts if you just want a background task performed by another app

Broadcast Receiver not receiving intent from another app in android 11

Solved. Here are some points:

  1. In App B (receiver), the permission needs to be also declared on top of the manifest, in <permission> tag. I missed this one.
  2. Category is not necessary. Also no need to declare the permission inside sendBroadcast()
  3. <uses-permission> in App A (sender) is necessary.
  4. ComponentName or package name (using setPackage()) needs to be mentioned in Android 11.

Here's the corrected code:

Here is the receiver App B:

Manifest:

<permission android:name="com.example.my_test.broadcast_permission"/>
...
<application>
...
<receiver android:name="com.example.my_test.TestReceiver"
android:permission="com.example.my_test.broadcast_permission">
<intent-filter>
<action android:name="com.example.my_test.receive_action"/>
</intent-filter>
</receiver>
...
</application>
...

Receiver class: No change.

Here is sender App A:

Manifest: No change

Sender code (inside MainActivity):

findViewById<Button>(R.id.button).setOnClickListener {
val intent = Intent("com.example.my_test.receive_action")
intent.component = ComponentName("com.example.my_test", "com.example.my_test.TestReceiver")
// if exact receiver class name is unknown, you can use:
// intent.setPackage("com.example.my_test")
intent.putExtra("data", 69)
sendBroadcast(intent)
}

How can I have one android app send a message to another to start it up in android O?

But apparently as of api 26, any broadcast receivers defined in the manifest that aren't particular system ones (ie, app specific ones like mine) get ignored

Not true. You're misunderstanding the restriction.

As of API 26, you can no longer receive implicit broadcasts with a Manifest-declared receiver. However, explicit broadcasts are exempt.

Target the receiver explicitly in your Intent:

Intent intent = new Intent("my_action");
intent.setComponent(new ComponentName("com.sister.packagename", "com.sister.packagename.Receiver");
sendBroadcast(intent);

You'll obviously need to use your own action and the proper package/class name, but that will allow you to keep your Manifest-defined receiver.

Make sure you're checking the action in that receiver's onReceive() method, though. By sending an explicit broadcast, Android ignores your intent filter and sends the intent anyway.



Related Topics



Leave a reply



Submit