Android--pendingintent & Intent

Source: Internet
Author: User

Pendingintent

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); "Note": This method is the old version of the API trial method, if the new version of the need to try Notifiication.builder

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.

the difference between pendingintent and intent: An Intent are something that's used right now; A pendingintent is something the to create an Intent on the future. You'll use a pendingintent with notifications, Alarmmanager, etc.

"A more detailed"

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. It is also possible to execute intent through the presence of the context in the pendingintent ( like turning off the message on a mobile phone, so you can open the information displayed on the notification bar via SMS notification). 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.

"See" http://blog.csdn.net/zeng622peng/article/details/6180190

Android--pendingintent & Intent

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.