Pages Navigation Menu

Coding is much easier than you think

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 Intentyou gave, they will execute the Intentwith their own permissions. But if you instead give the foreign application a Pending Intentyou created using your own permission, that application will execute the contained Intentusing 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 Intentaction that you want to perform, but at a later time. Think of it a putting an Intenton ice. The reason it’s needed is because an Intentmust be created and launched from a valid Contextin 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 Activityfrom a Notificationor a BroadcastReceiver.

By creating a PendingIntentyou want to use to launch, say, an Activitywhile you have the Contextto do so (from inside another Activityor 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. It€™s 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.

3 Comments

  1. Thank you for this info, it helps me understand what a pending intent is, but my problem is that I somehow put in a pending intent on my phone without realizing it and I don’t know what it is or how to cancel it. It is making the app run constantly and it runs my battery down fast. I tried stopping it but it keeps restarting. How do I find what the pending intent o. My phone is and cancel it?

  2. Thanks GOkul better suggest you to provide code snippet also to make to very clear.

  3. Thank you Gokul. That was very clear and very useful.