The difference between intent and pendingintent

Source: Internet
Author: User

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 time and Intent disappears as the activity disappears.
Pendingintent can be seen as intent packaging, usually through Getactivity,getbroadcast, getservice to get pendingintent instances, 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 to transfer data between activity, Sercvice, Broadcastreceiver, and pendingintent, generally used on notification, can be understood as the intent of deferred execution, Pendingintent is a wrapper over intent.

Java code
  1. Private void shownotify () {
  2. Notification notice=new Notification ();
  3. Notice.icon=r.drawable.icon;
  4. notice.tickertext="You have a new piece of information";
  5. Notice.defaults=notification.default_sound;
  6. notice.when=10l;
  7. //100 ms Delay, shock 250 ms, pause 100 milliseconds, then shake 500 milliseconds
  8. //notice.vibrate = new long[] {100, 250, 100, 500}; error?
  9. //notice.setlatesteventinfo (This, "notice", "Meeting", Pendingintent.getactivity (this, 0, NULL, 0));
  10. Notice.setlatesteventinfo (This, "notice", "Meeting", Pendingintent.getactivity (this, 0, new Intent (this, Activity2. class), 0)); //will jump to the page, not yet jump
  11. Notificationmanager manager= (Notificationmanager) getsystemservice (this. Notification_service);
  12. Manager.notify (0,notice);
  13. }
[Java]View Plaincopy
  1. Private void Shownotify () {
  2. Notification notice=New Notification ();
  3. Notice.icon=r.drawable.icon;
  4. notice.tickertext="You have a new piece of information";
  5. Notice.defaults=notification.default_sound;
  6. notice.when=10l;
  7. //100 ms Delay, shock 250 ms, pause 100 milliseconds, then shake 500 milliseconds
  8. //notice.vibrate = new long[] {100, 250, 100, 500}; error?
  9. //notice.setlatesteventinfo (This, "notice", "Meeting", Pendingintent.getactivity (this, 0, NULL, 0));
  10. Notice.setlatesteventinfo (This, "notice", "Meeting", pendingintent.getactivity (this, 0, new Intent ( This,activity2. Class), 0)); //will jump to the page, not yet jump
  11. Notificationmanager manager= (Notificationmanager) Getsystemservice (this.  Notification_service);
  12. Manager.notify (0,notice);
  13. }


1. Example of sending SMS to Android in GSM network

Java code
    1. String msg ="Hello, beauty";
    2. String number = "135****6784";
    3. Smsmanager SMS = Smsmanager.getdefault ();
    4. pendingintent pi = pendingintent.getbroadcast (smsactivity.   This,0,new Intent (...),0);
    5. Sms.sendtextmessage (number, null, MSG, pi, null);
    6. Toast.maketext (smsactivity.   This,"Send Success", Toast.lenght_long). Show ();
[Java]View Plaincopy
    1. String msg ="Hello, beauty";
    2. String number = "135****6784";
    3. Smsmanager SMS = Smsmanager.getdefault ();
    4. pendingintent pi = pendingintent.getbroadcast (smsactivity.   This,0,new Intent (...),0);
    5. Sms.sendtextmessage (number, null, MSG, PI, null);
    6. Toast.maketext (smsactivity.   This,"Send Success", Toast.lenght_long). Show ();


Code explanation
Pendingintent is a description of the intent, we can give this description to other programs, other programs according to this description at the other time to do what you 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 you Rself, the equivalent of pendingintent represents 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) in the parameter explanation:
1) pendingintent Sentintent: When the text message is issued, the success of Sendintent will be the description of its internal intent broadcast out, Otherwise, the error code and through the android.app.PendingIntent.OnFinished callback, this parameter is best not empty, otherwise there will be a potential problem of resource waste;
2) Pendingintent deliveryintent: is a pendingintent broadcast when the message has been delivered to the addressee.
Looking at the Pendingintent class, you can see many of the send functions, that is, Pendingintent is doing the related operations that are assigned.

The difference between intent and pendingintent

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.