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:
- In App B (receiver), the permission needs to be also declared on top of the manifest, in
<permission>
tag. I missed this one. Category
is not necessary. Also no need to declare the permission insidesendBroadcast()
<uses-permission>
in App A (sender) is necessary.ComponentName
or package name (usingsetPackage()
) 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
Android Webview with Garbled Utf-8 Characters
Update Listview Dynamically with Adapter
How to Start Android Application Info Screen Programmatically
Android How to Turn on Do Not Disturb (Dnd) Programmatically
Get JSON Data from Url Using Android
Custom Cut/Copy Action Bar for Edittext That Shows Text Selection Handles
Disable Edittext Blinking Cursor
Difference Between APK (.Apk) and App Bundle (.Aab)
Android: Getting "Manifest Merger Failed" Error After Updating to a New Version of Gradle
How to Represent a "Many to Many" Relation with Android Room When Column Names Are Same
Onsharedpreferencechanged Not Fired If Change Occurs in Separate Activity
How to Set Android_Sdk_Home Environment Variable
Is There a Simple Example of the Popupwindow Class Using Android V2.0
Error :: Duplicate Files During Packaging of APK
What File System Path Is Used by Android's Context.Openfileoutput()