I am a newbie to Android. I read the Android Documentation but I still need some more clarification. Can anyone tell me what exactly a
PendingIntent is a token that you give to a foreign application (e.g.
AlarmManager, Home Screen
AppWidgetManager, or other 3rd party applications), which allows the foreign application to use your application's permissions to execute a predefined piece of code.
If you give the foreign application an Intent, it will execute your
Intent with its own permissions. But if you give the foreign application a
PendingIntent, that application will execute your
Intent using your application's permission.
Intents are the standard messaging mechanism in Android that expresses the user’s intention to perform some work. They allow you to interact with other components defined by you or by the Android operating system.
Dial a phone number or answer a phone call
They are used in both ways
1) by you to call a component
2)by the system to notify you of some event.
The logical workflow of creating an intent is usually as follows:
Real Life Example: Let's say I wake up in the morning and I "INTEND" to go to the washroom. I will first have to THINK about going to the washroom, but that DOESN'T really gets me to the washroom. I will then have to tell my brain to get out of bed first, then walk to the washroom, and then release, then go and wash my hands, then go and wipe my hands. Once I know where I'm going I SEND the command to begin and my body takes action.
PendingIntent 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.
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.
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
BroadcastReceiver.By creating a
PendingIntent you want to use to launch, say, an Activity while you have the
Context to do so (from inside another Activity or Service)
Continuing from the real-life example: let's say I want to take a shower but I want to shower AFTER I brush my teeth and eat breakfast. So I know I won't be showering for at least 30-40 minutes. I still have in my head that I need to prepare my clothes, and then walk up the stairs back to the bathroom, then undress and then shower. However, this will not happen until 30-40 minutes have passed. Now I have a PENDING intent to shower. It is PENDING for 30-40 minutes.
That is pretty much the difference between a Pending Intent and a Regular Intent. In short:
Regular Intent ->
DOES NOT REQUIRE PENDING INTENT TO BE MADE
Pending Intent ->
REQUIRES A REGULAR INTENT TO BE CREATED
Intents are of two types- Explicit and Implicit
Explicit Intent: When your application is aware of which component to call to perform some action
Implicit Intent: When your application is not aware of which component can exactly perform your desired action. For Ex, If you simply say that you want to display a URL, the system decides what component will fulfill the intention.
For more better and clear idea about Intents. Vist below links