pendingintent literal meaning: waiting, intent not determined.
Pendingintent object, use the static method of the method class:
getActivity(Context, int, Intent, int)------->跳转到一个activity组件、
getBroadcast(Context, int, Intent, int)------>打开一个广播组件
getService(Context, int, Intent, int)-------->打开一个服务组件。
The 3 behaviors and parameters corresponding to intent are 4, the third and the first, the second is the fourth one and the second one. As you can see, to get this object, you must pass in a intent as a parameter, and you must have the context as a parameter.
pendingintent is a special kind of 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.
main use places and examples: Notification notificatio sent, short message smsmanager send and alarm alarmmanager execution, etc.
Intent English means intention, pending means something that is about to happen or come.
Pendingintent This class is used to handle what is about to happen. For example, in the notification notification for the jump page, but not immediately jump.
Intent is started in a timely manner, Intent disappears as the activity disappears.
Pendingintent can be seen as a wrapper over intent, usually through Getactivity,getbroadcast, GetService to get an instance of Pendingintent, The current activity does not immediately start the intent it contains, but instead calls the intent when the pendingintent is executed externally. Because the context of the current app is saved in the pendingintent, it gives the external app the ability to execute the Intent in the pendingintent like the current app, even if the current app doesn't exist at the time of execution. Intent can also be executed through the presence of the context in the pendingintent. You can also handle the operations after intent execution. Often used with Alermanger and Notificationmanager.
Intent is generally used for data transfer between activity, Sercvice, Broadcastreceiver, and pendingintent, generally used on notification, The intent,pendingintent that can be understood as deferred execution is a wrapper over intent.
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
Notification Notification = new Notification (icon, null, when);//create a new notification, 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);//setlatesteventinfo to set the event that clicked on the notification
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.
1 Private voidshownotify () {2Notification notice=NewNotification (); 3notice.icon=R.drawable.icon; 4Notice.tickertext= "You have a new piece of information"; 5notice.defaults=Notification.default_sound; 6notice.when=10l; 7 //100 ms Delay, shock 250 ms, pause 100 milliseconds, then shake 500 milliseconds8 //notice.vibrate = new long[] {100, 250, 100, 500}; error? 9 //Notice.setlatesteventinfo (This, "notice", "Meeting", Pendingintent.getactivity (this, 0, NULL, 0)); TenNotice.setlatesteventinfo ( This, "notice", "Meeting", Pendingintent.getactivity ( This, 0,NewIntent ( This, Activity2.class), 0));//going to jump page, not yet jump OneNotificationmanager manager= (Notificationmanager) Getsystemservice ( This. Notification_service); AManager.notify (0, notice); -}
1 Private voidshownotify () {2Notification notice=NewNotification (); 3notice.icon=R.drawable.icon; 4Notice.tickertext= "You have a new piece of information"; 5notice.defaults=Notification.default_sound; 6notice.when=10l; 7 //100 ms Delay, shock 250 ms, pause 100 milliseconds, then shake 500 milliseconds8 //notice.vibrate = new long[] {100, 250, 100, 500}; error? 9 //Notice.setlatesteventinfo (This, "notice", "Meeting", Pendingintent.getactivity (this, 0, NULL, 0)); TenNotice.setlatesteventinfo ( This, "notice", "Meeting", Pendingintent.getactivity ( This, 0,NewIntent ( This, Activity2.class), 0));//going to jump page, not yet jump OneNotificationmanager manager= (Notificationmanager) Getsystemservice ( This. Notification_service); AManager.notify (0, notice); -}
11. Example of sending SMS to Android in GSM network2 3 Java Code4String msg = "Hello, beauty"; 5String number = "135****6784"; 6Smsmanager SMS =Smsmanager.getdefault (); 7 8pendingintent pi = pendingintent.getbroadcast (smsactivity. This, 0,NewIntent (...), 0); 9Sms.sendtextmessage (number,NULL, MSG, PI,NULL); TenToast.maketext (smsactivity. This, "Send Success", Toast.lenght_long). Show ();
1 String msg = "Hello, beauty" 2 String number = "135****6784" ; 3 smsmanager SMS = Smsmanager.getdefault (); 4 5 pendingintent pi = Pendingintent.getbroadcast (Smsactivity. This , 0,new Intent (...), 0); 6 sms.sendtextmessage (number, null , MSG, pi, null ); 7 toast.maketext (smsactivity. This , "Send succeeded", Toast.lenght_long). Show ();
Code explanation
Pendingintent is a intent description, we can give this description to another program, Other programs according to this description at a later time do what you plan to do (by giving a pendingintent to another application, you is granting it the right to perform the Operation you has specified as if the other application is yourself, which is equivalent to pendingintent on behalf of intent). In this case, the other program is to send a text message program, SMS sent after the success of the intent broadcast out.
function Smsmanager.sendtextmessage (string destinationaddress, String scaddress, String text, Pendingintent sentintent, pendingintent deliveryintent) parameter interpretation:
1) pendingintent Sentintent: When the text message is issued, the success of Sendintent will be the description of its internal intent broadcast out, Otherwise, generate error code and callback through android.app.PendingIntent.OnFinished, this parameter is best not empty, otherwise there will be a potential problem of resource wasting;
2) pendingintent deliveryintent: A pendingintent broadcast after the message has been delivered to the recipient.
Viewing the Pendingintent class can see many of the send functions, that is, Pendingintent is doing the assigned related operations.
A good example of pendingintent is:
SMS System example Smsmanager method for sending 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.
//////////////////////////////////////////////////////////////////////////////////////////////
the role and examples of pendingintent and the difference between intent and
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
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.
In-depth understanding of pendingintent in Android