Differencebetween Sendstickybroadcast and Sendbroadcast in Android

differences between different types of broadcasts in android

You can compare a sticky broadcast with a sticky note. Someone posts it and you can read when you pass by/your application starts - regardless of when it was posted.

An ordered broadcast is like passing a note - it passes from person/application to person/application. Anywhere in the chain the recipient can elect to cancel the broadcast preventing the rest of the chain from seeing it.

A normal broadcast.. well, just sends to everyone that's allowed & registered to listen to it.

There's a variation of broadcasts that only allow receivers registered in a running application to listen to them - i.e. a receiver in your AndroidManifest.xml will not trigger for these Intents.

An update regarding sendStickyBroadcast:

This method was deprecated in API level 21. Sticky broadcasts should
not be used. They provide no security (anyone can access them), no
protection (anyone can modify them), and many other problems. The
recommended pattern is to use a non-sticky broadcast to report that
something has changed, with another mechanism for apps to retrieve the
current value whenever desired.

Source

What is a Sticky Broadcast?

If an Activity calls onPause with a normal broadcast, receiving the Broadcast can be missed. A sticky broadcast can be checked after it was initiated in onResume.

Update 6/23/2020

Sticky broadcasts are deprecated.

See sendStickyBroadcast documentation.

This method was deprecated in API level 21.

Sticky broadcasts should not be used. They provide no security (anyone can access them), no protection (anyone can modify them), and many other problems. The recommended pattern is to use a non-sticky broadcast to report that something has changed, with another mechanism for apps to retrieve the current value whenever desired.

Implement

Intent intent = new Intent("some.custom.action");
intent.putExtra("some_boolean", true);
sendStickyBroadcast(intent);

Resources

  • Related post: What is the difference between sendStickyBroadcast and sendBroadcast in Android?

  • See removeStickyBroadcast(Intent), and on API Level 5 +, isInitialStickyBroadcast() for usage in the Receiver's onReceive.

Sticky Broadcast Receivers - how do they work?

Are Broadcast Receivers sticky or Intents sticky?

Broadcast Intents sent via sendStickyBroadcast() are sticky, if you hold the proper permission to send such broadcasts.

Is sendStickyBroadcast(intentA) is the only way to make intentA sticky?

Yes.

If yes, what would happen if I call both sendStickyBroadcast(intentA) and sendBroadcast(intentA) ? Will the intentA be available for future receivers?

AFAIK, the behavior is undocumented. I would not recommend using that pattern.

Why is the onReceive() method executed & the intent returned by registerReceiver() not NULL, since it was not a sendStickyBroadcast() ?

Perhaps you previously sent a sticky broadcast for that same action string. Reboot your device or emulator and try your test again.

Knowing about Sticky intent in Android

Intent - is a message passing mechanism between components of Android, except for Content Provider. You can use Intent to start any
component.

Sticky Intent - Sticks with Android, for future broadcast listeners. For example if BATTERY_LOW event occurs then that Intent
will stick with Android so that any future requests for
BATTERY_LOW, will return the Intent.

Pending Intent - If you want some one to perform any Intent operation at future point of time on behalf of you, then we will use
Pending Intent.

connectivity change receiver gets false intents in android

The system is likely generating sticky broadcast android.net.conn.CONNECTIVITY_CHANGE intents. Sticky broadcasts stay around after they are sent. Whenever you register a BroadcastReceiver to listen for broadcasts that are sticky, the most recently broadcasted intent will be passed to the receiver immediately when registerReceiver() is called.

An easy way to test whether or not the android.net.conn.CONNECTIVITY_CHANGE broadcast is sticky would be to check the return value of your registerReceiver() call in onResume()

netReceiver = new ConnectStatusReceiver();
IntentFilter netFilter = new IntentFilter();
netFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
Intent intent = registerReceiver(netReceiver, netFilter);
if (intent != null) {
// broadcast is sticky
}

If you are able to determine that the broadcasts are indeed sticky, then what you are experiencing is intended behavior.

See what is the difference between sendStickyBroadcast and sendBroadcast in Android.



Related Topics



Leave a reply



Submit