Android pendingInten Usage Details
PendingIntent literal meaning: waiting, undecided Intent.
To get a pendingIntent object, use the static method of the method class.getActivity(Context, int, Intent, int),getBroadcast(Context, int, Intent, int),GetService (Context, int, Intent, int) corresponds to three Intent behaviors, jump to an activity component, open a broadcast component, and open a service component.
There are four parameters. The third and first parameters are important, followed by the Fourth and second parameters. As you can see, to get this object, you must input an Intent as the parameter and context as the parameter.
PendingIntent is a special Intent. The main difference is that the execution of the Intent is immediate, and the execution of the pendingIntent is not immediate. The operation executed by pendingIntent is essentially an Intent operation that involves parameter data transmission, but the purpose of using pendingIntent is that the execution of the Intent operation contained in it must meet certain conditions.
The main scenarios and examples are as follows: sending a notification to Notificatio, sending a short message to SmsManager, and executing an alarm to AlarmManager.
Android status bar Notification)
To view a message, drag the status bar to the bottom of the screen to view the message.
Steps:
1. Get the notification manager icationicationmanager, which is also a system service
2. Create Notification notification = new Notification (icon, null, when );
3. set parameters for new notifications (such as sound, vibration, and flashing)
4. Add a new notification to the notification manager.
The code for sending a message is as follows:
// Obtain the notification Manager
Icationicationmanager mNotificationManager = (NotificationManager) getSystemService (Context. icationication_service)
Int icon = android. R. drawable. stat_policy_chat;
Long when = System. currentTimeMillis (); // The time when the notification occurred is the current time of the System.
// Create a notification and specify its icon and title
Notification notification = new Notification (icon, null, when); // The first parameter is the icon, the second parameter is the short title, and the third parameter is the Notification time.
Notification. defaults = Notification. DEFAULT_SOUND; // send the default sound
Notification. flags | = Notification. FLAG_AUTO_CANCEL; // the notification is automatically cleared after the Notification is clicked.
Intent openintent = new Intent (this, OtherActivity. class );
PendingIntent contentIntent = PendingIntent. getActivity (this, 0, openintent, 0); // The openintent intent is sent to the system when a message is clicked.
Notification. setLatestEventInfo (this, "title", "I am content", contentIntent );
Micationicationmanager. Y (0, notification); // The first parameter is the unique identifier of the custom notification.
The focus is on the last parameter of the setLatestEventInfo () method !!!! It is a PendingIntent !!!!!!!!!
PendingIntent is used here (pend is intended to be TBD, meaning uncertain)
PendingIntent can be considered as a packaging of Intent. PendingIntent mainly holds the information of the encapsulated Intent and the Context of the current Application. Because PendingIntent stores the Context of the current Application, it grants the Intent with the execution of the program, even if the current Application does not exist during execution, intent can also be executed through Context in PendingIntent.
A good example of PendingIntent:
SmsManager is used to send text messages:
SendTextMessage (destinationAddress, scAddress, text, sentIntent, deliveryIntent );
First parameter: destinationAddress the recipient's mobile phone number
The second parameter: The scAddress SMS center number is generally set to null.
Third parameter: text message content
The fourth parameter: sentIntent determines whether the text message is successfully sent. If you do not have a SIM card or the network is interrupted, you can use this itent to determine. Pay attention to whether the "send" action is successful. In other words
The fifth parameter: deliveryIntent receives this deliveryIntent message when it is sent to the recipient. That is, the result after "sending" is emphasized.
That is to say, sentIntent and deliveryIntent are activated only when the message "sent successfully" and "received by the recipient. This is equivalent to the delay in Intent execution.
The preceding two examples show that PendingIntent is an Intent that can be executed under certain conditions. Compared with Intent, PendingIntent has its own Context object, in this way, he does not have to rely on an activity to exist.
//////////////////////////////////////// //////////////////////////////////////// //////////////
This article mainly introducesFunctions of PendingIntentAndExampleAndDifferences from IntentFor the code in this article, see AndroidDemo @ GoogleCode.
1. PendingIntent
It is a delay intent, mainly usedExecute a specific Action after an event is completed.. PendingIntent includes Intent and Context, so even if the program to which the Intent belongs ends, PendingIntent is still valid and can be used in other programs.
It is often used in the notification bar and SMS sending system.
PendingIntent is generally passed to an instance as a parameter. After an operation is completed on the instance, the Action on the PendingIntent is automatically executed. You can also manually execute the Action through the send function of the PendingIntent, you can also set OnFinished in the send function to indicate the action to be executed after successful sending.
2. PendingIntent example
A. System notification bar
Icationicationmanager nm = (NotificationManager) getSystemService (Context. NOTIFICATION_SERVICE); int icon = android. r. drawable. stat_policy_chat; long when = System. currentTimeMillis () + 2000; Notification n = new Notification (icon, "Notification bar demo reminder", when); n. defaults = Notification. DEFAULT_SOUND; n. flags | = Notification. FLAG_AUTO_CANCEL; Intent openintent = new Intent (this, DemoList. class); PendingIntent pi = PendingIntent. getActivity (this, 0, openintent, PendingIntent. FLAG_CANCEL_CURRENT); n. setLatestEventInfo (this, "notification bar demo reminder title", "notification bar demo reminder text", pi); nm. notify (0, n );
SetLatestEventInfo indicates setting the event for clicking the notification.
B. Text message system example
Text message system example code
The above two PendingIntent sentPI and deliveredPI will be broadcast when the SMS is successfully sent and the recipient accepts the message.
3. Differences between Intent and PendingIntent
A. Intent is used immediately, while PendingIntent can be triggered after the event occurs, and PendingIntent can be cancel
B. Intent is terminated after the program ends, while PendingIntent is still valid after the program ends.
C. PendingIntent comes with Context, and Intent needs to run in a specific Context
D. Intent runs in the original task and PendingIntent runs in the new task.
//////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// ///////////////////////////////////
PendingIntent describes Intent and its final behavior.
You can use the getActivity (Context context, int requestCode, Intent intent, int flags) series to obtain a PendingIntent object for starting an Activity from the system,
You can use the getService (Context context, int requestCode, Intent intent, int flags) method to obtain a PendingIntent object for starting a Service from the system.
You can use the getBroadcast (Context context, int requestCode, Intent intent, int flags) method to obtain a PendingIntent object for broadcast to the Intent of BroadcastReceiver.
The returned PendingIntent can be submitted to another application for further processing. Here, you can process the Intent and its final behavior described in PendingIntent later.
When you submit PendingIntent to another program for processing, PendingIntent still has the permission of the original PendingIntent Program (with the same permissions and identity ). when you get a PendingIntent from the system, you must be very careful. For example, if the Intent destination is your own component (Activity/Service/BroadcastReceiver), You 'd better display the specified component name in the Intent, to ensure that the Intent can reach the destination, otherwise the Intent may not know where to send it. A PendingIntent is an object reference of a token (node, which should be Linux or C \ C ++) in the Android system. It describes some data that will be used for retrieve (here, the Data describes Intent and its final behavior ).
This means that even if the original PendingIntent process ends, PendingIntent itself still exists and can be used in other processes (other programs submitted by PendingIntent. if I extract a PendingIntent from the system, and the system has a PendingInent equivalent to the PendingIntent you described, the system will directly return a PendingIntent with the same token as the PendingIntent, instead of a new token and PendingIntent. However, when you extract PendingIntent, use the FLAG_CANCEL_CURRENT parameter to remove the old PendingIntent's first cancel (). In this way, the pendingInten and its token are new.
With the FLAG_UPDATE_CURRENT parameter, the new Intent can update the Intent object data in the PendingIntent, for example, update the Extras in the Intent. In addition, we can also remove PendingIntent's cancel () from the system by calling PendingIntent's original process.