Knowing about Sticky intent in Android

Android

Android Problem Overview


In android there are 3 kinds of Intents,

  1. Intent,
  2. Sticky Intent,
  3. Pending intent.

so What is sticky intent?

Android Solutions


Solution 1 - 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.

Solution 2 - Android

An intent that is used with sticky broadcast, is called as sticky intent. This intent will stick with android system for future broadcast receiver requests.

OR

sendStickyBroadcast() performs a sendBroadcast(Intent) known as sticky, i.e. the Intent you are sending stays around after the broadcast is complete, so that others can quickly retrieve that data through the return value of registerReceiver(BroadcastReceiver, IntentFilter). In all other ways, this behaves the same as sendBroadcast(Intent). One example of a sticky broadcast sent via the operating system is ACTION_BATTERY_CHANGED. When you call registerReceiver() for that action -- even with a null BroadcastReceiver -- you get the Intent that was last broadcast for that action. Hence, you can use this to find the state of the battery without necessarily registering for all future state changes in the battery.

Solution 3 - Android

Pending Intent: Pending Intent is actually an object which wraps an Intent to do some future work by another app.

It lets us pass a future Intent to another application and allows that application to execute that Intent as if it had the same permissions as our application, whether or not our application is still around when the Intent is eventually invoked.

A PendingIntent is generally used in cases were an AlarmManager needs to be executed or for Notifications. A PendingIntent provides a mean for applications to work, even after their process exits.

PendingIntent uses the following methods to handle the different types of intents:

PendingIntent.getActivity() : Retrieve a PendingIntent to start an Activity
PendingIntent.getBroadcast() : Retrieve a PendingIntent to perform a Broadcast
PendingIntent.getService() : Retrieve a PendingIntent to start a Service

Example :

Intent intent = new Intent(this, SomeActivity.class);
 
// Creating a pending intent and wrapping our intent
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
try {
    // Perform the operation associated with our pendingIntent
    pendingIntent.send();
} catch (PendingIntent.CanceledException e) {
    e.printStackTrace();
}

Intent: Intent is basically a message passing mechanism between different components of Android, except for Content Provider. You can use intent to start any component in Android.

Sticky Intent: These are the Intents which sticks with Android for future broadcast listener.

Sticky Intent is also a type of Intent which allows communication between a function and a service sendStickyBroadcast(), performs a sendBroadcast(Intent) known as sticky, the Intent you are sending stays around after the broadcast is complete, so that others can quickly retrieve that data through the return value of registerReceiver(BroadcastReceiver, IntentFilter). In all other ways, this behaves the same as sendBroadcast(Intent).

One example of a sticky broadcast sent via the operating system is ACTION_BATTERY_CHANGED. When you call registerReceiver() for that action — even with a null BroadcastReceiver — you get the Intent that was last Broadcast for that action. Hence, you can use this to find the state of the battery without necessarily registering for all future state changes in the battery.

Solution 4 - Android

>Intent : Intent is an asynchronous message which is use to communicate between the components in android , except Content Provider. for example you can start activity by startActivity(Intent intent);

>Sticky Intent : sticky intents are associated with the android system for the future broadcast events.

>Pending Intent : Those intent which you want to trigger at some time in future when you application is not alive.

Solution 5 - Android

An intent that is used with sticky broadcast, is called as sticky intent. This intent will stick with android system for future broadcast receiver requests.

Solution 6 - Android

Sticky Intent allows a communication between function and a service sendStickyBroadcast() performs a sendBroadcast(Intent) know as sticky, the Intent you are sending stays around after the broadcast is complete so that others can quickly retrieve that data through the return value of registerReceiver(BroadcastReceiver, IntentFilter). In all other ways, this works the same as sendBroadcast(Intent).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShetty Suresh Babu.View Question on Stackoverflow
Solution 1 - AndroidArun AntoneyView Answer on Stackoverflow
Solution 2 - AndroidUmairView Answer on Stackoverflow
Solution 3 - AndroidDeepakPanwarView Answer on Stackoverflow
Solution 4 - AndroidNitendra ThakurView Answer on Stackoverflow
Solution 5 - AndroidChunsheng WeiView Answer on Stackoverflow
Solution 6 - AndroidSharanjeet KaurView Answer on Stackoverflow