pendingintent literal meaning: waiting, intent not determined.
To get a Pendingintent object that uses a static method of the getActivity(Context, int, Intent, int) getBroadcast(Context, int, Intent, int) method class, getService(Context, int, Intent, int) 分别对应着Intent的3个行为,跳转到一个activity组件、打开一个广播组件和打开一个服务组件。
参数有4个,比较重要的事第三个和第一个,其次是第四个和第二个。可以看到,要得到这个对象,必须传入一个Intent作为参数,必须有context作为参数。
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 action performed by Pendingintent is essentially the intent operation that the parameter passes in, but the purpose of using pendingintent is that the execution of the intent operation it contains needs to meet certain conditions.
The main use places and examples: Notification notificatio send, Short message smsmanager send and alarm alarmmanager execution etc.
Status bar notifications for Android (Notification)
If you need to see the message, you can drag the status bar to the bottom of the screen to view the message.
Steps:
1 Gets the notification Manager Notificationmanager, which is also a system service
2 Build Notification Notification Notification = new Notification (icon, null, when);
3 Setting parameters for new notifications (e.g. sound, vibration, flashing lights )
4 adding new notifications to the notification Manager
The code to send the message is as follows:
Get Notification Manager
Notificationmanager Mnotificationmanager = (notificationmanager) getsystemservice (Context.notification_service)
int icon = Android. R.drawable.stat_notify_chat;
Long when = System.currenttimemillis ();//notification occurs at the current time of the system
Create a new notification that specifies its icon and caption
Notification Notification = new Notification (icon, null, when);//The first parameter is an icon , the second parameter is a short tip title , and the third is the notification time
Notification.defaults = notification.default_sound;//emits default sound
Notification.flags |= notification.flag_auto_cancel;//automatically clear notifications after clicking notifications
Intent openintent = new Intent (this, otheractivity.class);
Pendingintent contentintent = pendingintent.getactivity (this, 0, openintent, 0);//When a message is clicked, the openintent intent is sent to the system
Notification.setlatesteventinfo (This, "title ", "I am content ", contentintent);
Mnotificationmanager.notify (0, notification);//The first parameter is a custom notification unique identifier
The focus is on the last parameter of the Setlatesteventinfo () Method!!! It is a pendingintent!!!!!!!!!
Here used to pendingintent (pend meant to be determined, indeterminate meaning )
Pendingintent can be seen as a packaging for intent. Pendingintent primarily holds information about the intent it wraps and the Context of the current application . It is due to the fact that the current application context is preserved in the pendingintent, giving it the ability to execute a intent with his program , even if the current Application no longer exists, can also be carried out through the presence of the context of the pendingintent Intent.
A good example of pendingintent is:
Smsmanager the way to send SMS:
Sendtextmessage (destinationaddress, scaddress, text, sentintent, deliveryintent);
First parameter:destinationaddress each other's mobile phone number
Second parameter:scaddress SMS Center number is generally set to empty
Third parameter:text text message content
The fourth parameter:sentintent determine whether the text message is sent successfully, if you do not have a SIM card, or network interruption, you can use this itent to judge. Note the emphasis is on whether the "send" action was successful. So as for whether or not the other party received
Fifth parameter:deliveryintent When a text message is sent to the recipient, the deliveryintent is received. The result of "send" is emphasized.
This means that the two Intent of sentintent and deliveryintent are activated when the message is sent successfully and the recipient receives this message. This is also equivalent to delaying the execution of the Intent
As the above two examples can be understood,Pendingintent is a intent that can be executed under certain conditions, and its advantage over intent is that it carries a context object so that he does not have to rely on an activity to exist.
//////////////////////////////////////////////////////////////////////////////////////////////
This article mainly introduces the function and example of pendingintent and the difference between intent , the code in this article see [email protected].
1. Pendingintent effect
The intent, which is known literally as a delay, is used primarily to execute a specific action after an eventhas been completed. Pendingintent contains the intent and context, so even if intent belongs to the end of the program, Pendingintent is still valid, can be used in other programs.
Commonly used in the notification bar and SMS delivery system.
Pendingintent is generally passed as a parameter to an instance, and the action on Pendingintent is executed automatically after the instance completes an operation, or it can be executed manually by Pendingintent's send function. You can also set onfinished in the Send function to indicate the action performed after the send succeeds.
2, pendingintent example
A. System notification bar
Notificationmanager nm = (Notificationmanager) getsystemservice (context.notification_service); int icon = Android. R.drawable.stat_notify_chat;long when = System.currenttimemillis () + 2000; Notification n = new Notification (icon, "Notification bar demo alert", 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 alert title", "Notification bar demo alert text", pi); nm.notify (0, N);
Setlatesteventinfo to set the event that clicked on the notification
B. Example of SMS system
SMS System Example code
Private final static string send_action = "SEND";p rivate final static string delivered_action = "Delivered";p Rivate V OID sendsms (string receiver, string text) {Smsmanager s = smsmanager.getdefault (); Pendingintent SENTPI = pendingintent.getbroadcast (this, 0, new Intent (Send_action), Pendingintent.flag_cancel_current); Pendingintent deliveredpi = pendingintent.getbroadcast (this, 0, new Intent (Delivered_action), Pendingintent.flag_cancel_current); Send Complete Registerreceiver (new Broadcastreceiver () {@Override public void onreceive (context context, Inten T intent) {switch (Getresultcode ()) {case Activity.RESULT_OK:Toast.makeTex T (Getbasecontext (), "Send success!", Toast.length_short). Show (); Break Case SmsManager.RESULT_ERROR_GENERIC_FAILURE:Toast.Maketext (Getbasecontext (), "Send Failed because generic failure cause.", Toast.length_sh ORT). Show (); Break Case SmsManager.RESULT_ERROR_NO_SERVICE:Toast.makeText (Getbasecontext (), "Send Failed because SERVICE is currently unavailable. ", Toast.length_short). Show (); Break Case SmsManager.RESULT_ERROR_NULL_PDU:Toast.makeText (Getbasecontext (), "Send Failed because no PDU pro vided. ", Toast.length_short). Show (); Break Case SmsManager.RESULT_ERROR_RADIO_OFF:Toast.makeText (Getbasecontext (), "Send Failed because RADIO was Explicitly turned off. ", Toast.length_short). Show (); Break Default:Toast.makeText (Getbasecontext (), "Send Failed.", Toast.length_short). Show (); Break }}}, new Intentfilter (send_action)); The other party accepts the completion registerreceiver (new Broadcastreceiver () {@Override public void onreceive (context context, Int Ent intent) {switch (Getresultcode ()) {case Activity.RESULT_OK:Toast.makeT Ext (Getbasecontext (), "Delivered success!", Toast.length_short). Show (); Break Default:Toast.makeText (Getbasecontext (), "Delivered failed!", Toast.length_short). Show (); Break }}}, new Intentfilter (delivered_action)); Send SMS, SENTPI and DELIVEREDPI will be broadcast s.sendtextmessage (receiver, NULL, text, SENTPI, deliveredpi) respectively when the message is sent successfully and the other person accepts success;}
The above two pendingintent SENTPI and DELIVEREDPI will be broadcast when the message is sent successfully and the other party accepts success
3. The difference between intent and pendingintent
A. Intent is used immediately, and pendingintent can wait until the event is triggered, Pendingintent can cancel
B. Intent terminates at the end of the procedure and Pendingintent remains in force after the completion of the procedure
C. pendingintent comes with context, and intent needs to run within a context
D. Intent runs in the original task, Pendingintent runs in the new task
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////
Pendingintent is used to describe intent and its final behavior.
You can use the Getactivity (context context, int requestcode, Intent Intent, int flags) series method to get a Pendingintent object from the system to start an activity ,
A Pendingintent object for starting a service can be obtained from the system through the GetService (context context, int requestcode, Intent Intent, int flags) method
Available through Getbroadcast (context context, int requestcode, Intent Intent, int flags) method to obtain a Pendingintent object for intent broadcast to broadcastreceiver from the system.
The returned pendingintent can be submitted to another application and then continue processing. Here you can deal with the intent and its final behavior described in pendingintent later.
When you submit pendingintent to other programs for processing, Pendingintent still has the privileges (with the same permissions and identity) that the Pendingintent original program has. When you get a pendingintent from the system, you must be very careful. For example, in general, if the intent destination is your own component (Activity/service/broadcastreceiver), you might want to use the method of displaying the component name of the specified purpose in intent, To make sure intent end up with a purpose, otherwise intent may end up unaware of where it was sent. A pendingintent is an object reference to a token (node, which should be a Linux or c\c++ term) in the Android system, which describes some of the data that will be used for retrieve (here, This data describes the intent and its eventual behavior).
This means that even if the pendingintent process is over, pendingintent itself still exists, Can continue to be used in other processes (other programs that pendingintent is submitted to). If I'm extracting a pendingintent from the system, and there's a pendingintent equivalent pendinginent that you're describing, Then the system will return directly and the pendingintent is actually the same token as the pendingintent, not a new token and pendingintent. However, when you extract the Pendingintent, pass the flag_cancel_current parameter, let the old Pendingintent cancel (), so that the Pendinginten and its token is new.
With the flag_update_current parameter, you can let the new intent update the intent object data in the previous pendingintent, such as intent in the update extras. In addition, we can also call Pendingintent's Cancel () in Pendingintent's original process to remove it from the system.
An in-depth understanding of pendingintent in Android