Android basic understanding --PendingIntent

1. PendingIntent know

PendIntent actually Intent package , which has brought several questions:

  • Why should PendingIntent? And Intent What is the difference?
  • PendingIntent What are the main application scenarios?
  • Its interior is how to achieve?

The difference between 1.1 and Intent

  • IntentIt is intended meaning. Intent on Android mean is taken, which is a message object, by which the four components, the system can be conveniently Android communication, and to ensure decoupling.

    Intent can explain a purpose, carry an act and the corresponding data is sent to the target component.

  • PendingIntentIntent is to package, but it was not immediately perform an action, but certain conditions are met, or after certain events trigger the execution of the specified behavior

A component created the object is then passed to a PendingIntent B component, B in this PendingIntent send execution time, which is inside Intent will be sent, but received Intent of the C component will be considered A hair.
B Intent sent to the A's identity and authority

Our Activity If you set exported = false, if you use other applications Intent on not visit this Activity, but using PendingIntent is possible.

Namely: PendingIntent an action that will trigger the timing of the application to the other; let that application on their own behalf to perform that action (he gave permission)

2.2 acquire PendingIntent

PendingIntent for examples generally have acquired the following five methods, respectively Activity, Broadcast, Service

  • getActivity()
  • getActivities()
  • getBroadcast()
  • getService()
  • getForegroundService()

Their parameters are the same, four are: Context, , requestCode, Intent,flags corresponding to the context object, request code, the request is intended to denote a class and the data transfer start, the key flag.
The first three parameters common sign of a unique behavior, and the fourth parameter flags:

  • FLAG_CANCEL_CURRENT: If the current system already exists in a PendingIntent the same object, then it will have been the first PendingIntent canceled, then re-generate a PendingIntent object.
  • FLAG_NO_CREATE: If the same PendingIntent object does not exist in the current system, the system will not create the objects but directly PendingIntent return null, if before setting off, this time will be able to get to.
  • FLAG_ONE_SHOT: The PendingIntent action only once. After triggering the method PendingIntent object through the send (), PendingIntent will automatically call the cancel () destruction, then if you then call the send () method, the system will return a SendIntentException.
  • FLAG_UPDATE_CURRENT: If there is a PendingInent and PendingIntent peer you describe the system, the system will use the PendingIntent object, but will be updated with the new Intent Intent object data PendingIntent before, for example in the Extras update Intent

Note : two peer refers PendingIntent as their operation, and which are of the Intent action, data, categories, components, and flags are the same. But they may not be the same as the Intent Extra

2.3 usage scenarios

PendingIntent on usage scenarios mainly used for alarm, notification, the desktop component.

The general principle is: A B application to let application to help trigger a behavior, which is a cross-application communication, you need Android system as middlemen here is ActivityManager. A built PendingIntent application creation, during the creation of the PendingIntent to ActivityManager registered this PendingIntent, so even if application A dead when it wakes up again, as long as the same parameters, you can still get to that before the PendingIntent. When the A system API calls, such as PendingIntent AlarmManager.set (), is actually the permissions to the application B, at this time, the application B can be acquired from ActivityManager A set according to parameter information PendingIntent

Guess you like

Origin blog.csdn.net/weixin_43499030/article/details/90264915