Search code examples
androidandroid-intentbroadcastreceiverandroid-broadcast

Which Intents/Broadcasts could be "sticky" in Android?


Where can I find information which Intent/Broadcast can be sticky?

Example of a sticky broadcast sent via the operating system is ACTION_BATTERY_CHANGED. When I call registerReceiver() for that action with a null BroadcastReceiver — I get the Intent that was last Broadcast for that action.

Whenever I find the last value by:

//In Activity
val batteryIntent = registerReceiver(null, IntentFilter(Intent.ACTION_BATTERY_CHANGED)); //sticky
val level = batteryIntent?.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
val scale = batteryIntent?.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

This Intent: AudioManager.ACTION_HEADSET_PLUG works too, but some Intents don't work: LocationManager.MODE_CHANGED_ACTION, Intent.ACTION_POWER_CONNECTED BluetoothDevice.ACTION_ACL_CONNECTED, ... I know, some don't make sense, but result of these Intents are always null, even if there has been a change, why?

For ACTION_BATTERY_CHANGED there is information: "This is a sticky broadcast ..." in doc, but for ACTION_HEADSET_PLUG no.

Is exist any restriction or split to sticky / non-sticky or list which Intents work as sticky?
Why do some Intents work and others don't?

Thank you.


Solution

  • Where can I find information which Intent/Broadcast can be sticky?

    Look at the source code for the Android version of interest, searching for sendStickyBroadcast().

    Is exist any restriction or split to sticky / non-sticky or list which Intents work as sticky?

    Sticky broadcasts are sent using sendStickyBroadcast(). Technically, it is not tied to a specific Intent.