what is pending intent in android
A Pending Intent specifies an action to take in the future. It lets you pass a future Intent to another application and allow that application to execute that Intent as if it had the same permissions as your application, whether or not your application is still around when the Intent is eventually invoked.
It is a token that you give to a foreign application which allows the foreign application to use your application’s permissions to execute a predefined piece of code.
** UPDATE: Android Complete tutorial now available here.
If you give the foreign application an Intent, and that application sends/broadcasts the
Intent you gave, they will execute the
Intent with their own permissions. But if you instead give the foreign application a
Pending Intent you created using your own permission, that application will execute the contained
Intent using your application’s permission.
To perform a broadcast via a pending intent so get a PendingIntent via PendingIntent.getBroadcast(). To perform an activity via an pending intent you receive the activity via PendingIntent.getActivity().
It is an
Intent action that you want to perform, but at a later time. Think of it a putting an
Intent on ice. The reason it’s needed is because an
Intent must be created and launched from a valid
Context in your application, but there are certain cases where one is not available at the time you want to run the action because you are technically outside the application’s context (the two common examples are launching an
Activity from a
Notification or a
By creating a
PendingIntent you want to use to launch, say, an
Activity while you have the
Context to do so (from inside another
Service) you can pass that object around to something external in order for it to launch part of your application on your behalf.
A PendingIntent provides a means for applications to work, even after their process exits. Its important to note that even after the application that created the PendingIntent has been killed, that Intent can still run. A description of an Intent and target action to perform with it. Instances of this class are created with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), getService (Context, int, Intent, int); the returned object can be handed to other applications so that they can perform the action you described on your behalf at a later time.
By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself (with the same permissions and identity). As such, you should be careful about how you build the PendingIntent: often, for example, the base Intent you supply will have the component name explicitly set to one of your own components, to ensure it is ultimately sent there and nowhere else.
A PendingIntent itself is simply a reference to a token maintained by the system describing the original data used to retrieve it. This means that, even if its owning application’s process is killed, the PendingIntent itself will remain usable from other processes that have been given it. If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid, and can thus call cancel() to remove it.