Detailed analysis of differences between Android Intent and PendingIntent

Source: Internet
Author: User

PendingIntent was used in the previous example, which is similar to the previous Intent. Therefore, Baidu made a summary based on its own understanding:

Intent is an Intent that describes the intention to start an Activity, Broadcast, or Service. The main information it holds is the component (Activity, Broadcast, or Service) it wants to start ).

PendingIntent can be considered as a packaging of Intent. It can be called by other apps other than the current App. "Passive" or "Callback", but not "passive" or "Callback" in a strict sense ". In short, the current App cannot use it to immediately start the Intent it wraps. However, when the external App executes this PendingIntent, it indirectly and practically calls the Intent. PendingIntent mainly holds the information of the encapsulated Intent and Context of the current App. Because PendingIntent stores the Context of the current App, it gives the external App a capability, so that the external App can execute the Intent in PendingIntent 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.

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:

• Private void showNotify (){
• Notification notice = new Notification ();
• Notice. icon = R. drawable. icon;
• Notice. tickerText = "you have a new message ";
• Notice. defaults = Notification. DEFAULT_SOUND;
• Notice. when = 10L;
• // After a delay of 100 milliseconds, the system shakes for 250 milliseconds. After a pause of 100 milliseconds, the system shakes for 500 milliseconds.
• // Notice. vibrate = new long []
{100,250,100,500 };
Error?
• // Notice. setLatestEventInfo (this, "notification", "meeting", PendingIntent. getActivity (this, 0, null, 0 ));
• Notice. setLatestEventInfo (this, "notification", "meeting", PendingIntent. getActivity (this, 0, new Intent (this, Activity2.class), 0 ));
// The page is about to jump. www.2cto.com is not yet redirected.
• Icationicationmanager manager = (NotificationManager) getSystemService (this. NOTIFICATION_SERVICE );
• Manager. Y (0, notice );
•}


1. Example of android text message sending in GSM network

Java code:

• String msg = "Hello, beautiful girl ";
• String number = "135 **** 6784 ″;
• SmsManager sms = SmsManager. getDefault ();

• PendingIntent pi = PendingIntent. getBroadcast (SmsActivity. this, 0, new Intent (...), 0 );
• Sms. sendTextMessage (number, null, msg, pi, null );
• Toast. makeText (SmsActivity. this, "sent successfully", Toast. LENGHT_LONG). show ();


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, the intent program should be broadcasted.
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.
 

From First_Wolf
 

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.