What is an Android PendingIntent?

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 PendingIntent is a token that you give to a foreign application (eg NotificationManager , 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.


INTENT

Intents are the standard messaging mechanism in Android that express 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.

Example Broadcast

  • a message
  • Start the camera
  • Start a service
  • Launch an activity
  • Display a web page or a list of contacts
  • 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:

  • Create the Intent
  • b. Add Intent options-> Ex. what type of intent we are sending to the OS or any attributes associated with that intent, such as a text string or something being passed along with the intent
  • c. RUN the Intent
  • 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 get 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

    A 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 until 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. I now 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

  • Slidenerd.com
  • Android Intent Tutorial
  • Some More

  • A Pending Intent is a token you give to some app to perform an action on your apps' behalf irrespective of whether your application process is alive or not.

    I think the documentation is sufficiently detailed: Pending Intent docs.

    Just think of use-cases for Pending Intents like (Broadcasting Intents, scheduling alarms) and the documentation will become clearer and meaningful.

    链接地址: http://www.djcxy.com/p/16226.html

    上一篇: Android布局文件中的“工具:上下文”是什么?

    下一篇: 什么是Android PendingIntent?