Differences between android Intent PendingIntent and android intent

Source: Internet
Author: User

Differences between android Intent PendingIntent and android intent

Meaning: intent indicates the intention. pending indicates the upcoming or coming thing.
The PendingIntent class is used to handle upcoming events. For example, in Notification, it is used to jump to a page, but not to jump to the page immediately.

Intent is started in time. intent disappears with the activity.
PendingIntent can be considered as a packaging of intent. Generally, the pendingintent instance is obtained through getActivity, getBroadcast, and getService. The current activity cannot start the intent contained in it immediately, instead, intent is called when pendingintent is executed externally. Because pendingintent stores the Context of the current App, it gives the external App the ability to execute the Intent in pendingintent just like the current App, even if the current App does not exist during execution, the Intent can still be executed through the Context in pendingintent. You can also process operations performed by intent. It is often used with alermanger and icationicationmanager.
Intent is generally used to transmit data between activities, Sercvice, and BroadcastReceiver, while Pendingintent is generally used in Notification. It can be understood as a delayed execution intent, and PendingIntent is a packaging of Intent.

Java code
  1. Private VoidShowpolicy (){
  2. Notification notice =NewNotification ();
  3. Notice. icon = R. drawable. icon;
  4. Notice. tickerText = "you have a new message ";
  5. Notice. defaults = Notification. DEFAULT_SOUND;
  6. Notice. when = 10L;
  7. // After a delay of 100 milliseconds, the system vibrate for 250 milliseconds. After a pause of 100 milliseconds, the system then shakes for 500 milliseconds.
  8. // Notice. vibrate = new long [] {100,250,100,500}; error?
  9. // Notice. setLatestEventInfo (this, "notification", "meeting", PendingIntent. getActivity (this, 0, null, 0 ));
  10. Notice. setLatestEventInfo (This, "Notice", "meeting", PendingIntent. getActivity (This, 0,NewIntent (This, Activity2.Class), 0); // you are about to jump to the page.
  11. Icationicationmanager manager = (NotificationManager) getSystemService (This. Icationication_service );
  12. Manager. Policy (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 message ";
  5. Notice. defaults = Notification. DEFAULT_SOUND;
  6. Notice. when = 10L;
  7. // After a delay of 100 milliseconds, the system vibrate for 250 milliseconds. After a pause of 100 milliseconds, the system then shakes for 500 milliseconds.
  8. // Notice. vibrate = new long [] {100,250,100,500}; error?
  9. // Notice. setLatestEventInfo (this, "notification", "meeting", PendingIntent. getActivity (this, 0, null, 0 ));
  10. Notice. setLatestEventInfo (this, "notification", "meeting", PendingIntent. getActivity (this, 0, new Intent (this, Activity2.class), 0); // The page is about to jump.
  11. Icationicationmanager manager = (NotificationManager) getSystemService (this. NOTIFICATION_SERVICE );
  12. Manager. Policy (0, notice );
  13. }


1. Example of android text message sending 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,NewIntent (...), 0 );
  5. Sms. sendTextMessage (number,Null, Msg, pi,Null);
  6. Toast. makeText (SmsActivity.This, "Sent successfully", 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, "sent successfully", Toast. LENGHT_LONG). show ();


Code explanation
PendingIntent is an Intent description. We can give this description to other programs, other programs will do what you want to do at the rest of the time according to this description (By giving a PendingIntent to another application, you are granting it the right to perform the operation you have specified as if the other application was yourself, it is equivalent to PendingIntent representing the Intent ). In this example, other programs send text messages. After the messages are sent successfully, intent should be broadcast.
Description of parameters in the SmsManager. sendTextMessage (String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent) function:
1) PendingIntent sentIntent: if the message is sent successfully, sendIntent broadcasts the intent of its internal description. Otherwise, an error code is generated and sent to android. app. pendingIntent. onFinished callback. It is best not to leave this parameter blank, otherwise there may be potential problems of resource waste;
2) PendingIntent deliveryIntent: The PendingIntent broadcast that is performed after the message has been delivered to the recipient.
View the PendingIntent class. You can see many Send functions, that is, PendingIntent is performing related operations.

Related Article

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.