How to Use Broadcast Receiver in Different Applications in Android

How to use Broadcast Receiver in different Applications in Android?

  1. Your <receiver> element needs to be a peer of your <activity> element, not a child.
  2. Your action string should NOT be in the android.intent.action namespace, unless you work for Google -- use ch.ifi.csg.games4blue.games.pacman.controller.BROADCAST or something like that instead
  3. 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:

  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)
}

In-Application BroadcastReceiver

There are a number of ways to do this. The first 2 I can think of are like this:

  1. 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 adding android:enabled="false" to the manifest entry for <receiver>.
    Now, when your app is running, in onResume() you want to enable the receiver. Use PackageManager.setComponentEnabledSetting() to do this. When your activity goes to the background, in onPause() you can disable the receiver again.

  2. 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 in onResume() call registerReceiver() with the appropriate intent filter. When the app goes to the background, in onPause() call unregisterReceiver() to remove it. The receiver will only receive calls to onReceive() while the app is in the foreground.



Related Topics



Leave a reply



Submit