How to use Broadcast Receiver in different Applications in Android?
- Your
<receiver>
element needs to be a peer of your<activity>
element, not a child. - Your action string should NOT be in the
android.intent.action
namespace, unless you work for Google -- usech.ifi.csg.games4blue.games.pacman.controller.BROADCAST
or something like that instead - Your
<intent-filter>
with your custom action needs to be placed on the<receiver>
, not the sending or receiving<activity>
See here for an example of implementing a manifest-registered broadcast receiver (for a system-broadcast 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.
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)
}
In-Application BroadcastReceiver
There are a number of ways to do this. The first 2 I can think of are like this:
If your
<receiver>
is declared in the manifest with an<intent-filter>
, you can enable it and disable it from your application so that it is only enabled when the app is in the foreground. To do this, initially set the receiver disabled by addingandroid:enabled="false"
to the manifest entry for<receiver>
.
Now, when your app is running, inonResume()
you want to enable the receiver. UsePackageManager.setComponentEnabledSetting()
to do this. When your activity goes to the background, inonPause()
you can disable the receiver again.Dynamically register and unregister the receiver. For this you don't need to declare the receiver in the manifest. In your application, create an instance of your
BroadcastReceiver
and inonResume()
callregisterReceiver()
with the appropriate intent filter. When the app goes to the background, inonPause()
callunregisterReceiver()
to remove it. The receiver will only receive calls toonReceive()
while the app is in the foreground.
Related Topics
Android Draw Route on a Mapview with Twoo Poi-S
Disable Home Button in Android Ics (4.0)
Checking My App Version Programmatically in Android Market
Bufferoverflowexception When Building Application
Picasso Loading of Image Spawned Inside Asynctask
Android Usb Host API and Usb Storage
Swap Fragment in an Activity via Animation
Android: Looking for a Drawarc() Method with Inner & Outer Radius
Android 4.0, Text on the Action Bar Never Shows
Android - Way to Appear Bordered Text on the Textview
Opencv Android Green Color Detection
Creating Different Layout for Android Phone and Tablet
Android Options Menu Icon Won't Display
Getactionview() of My Menuitem Return Null
Pass Arraylist Data into Soap Web Service in Android
How Can One Pull the (Private) Data of One's Own Android App