Reprint: Pendingintent
2010-11-16 00:03:55
Tags: pendingintent Intent smsmanager getbroadcast broadcastreveiver
Original works, allow reprint, please be sure to use hyperlinks in the form of the original source of the article, author information and this statement. Otherwise, the legal liability will be investigated. http://lbrant.blog.51cto.com/2400264/424154
Intent and pendingintent relationship, when beginners are confused, when using pendingintent , there will be strange problems, such as the inability to transfer data, unable to update the data, So I set the length of the family, coupled with some of my personal practice, summarized as follows, I hope to give you some help.
First look at the official explanation: an Intent was something that was used right now; a pendingintent is something the May create a intent in the future. You'll use a pendingintent with notifications, Alarmmanager, etc.
Intent Everyone is familiar with, is an intention, this intention indicates to start which Activity,service,pendingintent can be seen as a further encapsulation of Intent , It is a description of the Intent , we can give this description to other programs, other programs according to this description in the later time do you arrange to do things, here is an example of sending SMS SMS:
String msg = "Hello"; String number = "135****6784"; Smsmanager SMS = Smsmanager.getdefault (); pendingintent pi = pendingintent.getbroadcast (smsactivity.this,0,new Intent (XXX), 0); Sms.sendtextmessage (number, NULL, MSG, PI, null); Toast.maketext (Smsactivity.this, "Send Success", Toast.lenght_long). Show ();
Method Smsmanager.sendtextmessage (String destinationaddress, String scaddress, string text, Pendingintent sentintent, Pendingintent deliveryintent):
-
Pendingintent sentintent: When the text message is sent, the successful sendintent will broadcast its internal description of the intent , of course, after the failure will generate an error code, and through Android.app.PendingIntent.OnFinished the callback ("Callback");
-
Pendingintent deliveryintent: A Intent broadcast after the message has been transmitted to the addressee;
If your Broadcastreveiver register receives the corresponding message, you will receive the corresponding Intent, this time can be based on Intent action, the corresponding action, this is said in the the meaning of the future;
There are three static methods to obtain Pendingintent instances:
-
public static Pendingintent Getbroadcast (context context, int requestcode, Intent Intent, int flags)
-
public static Pendingintent getactivity (context context, int requestcode, Intent Intent, int flags)
-
public static Pendingintent GetService (context context, int requestcode, Intent Intent, int flags)
The flags parameter has three, and I think English is better understood:
The
Flag_one_shot:this pendingintent can is only used once. If set, after send () are called on it, it'll be automatically canceled for you and any future attempt to send through it would fail.
Flag_update_current:if The described pendingintent already exists, then keep it and its replace its extra data wi Th what's in this new Intent . This can is used if you is creating intents where only the extras is change, and don ' t care which any entities that received Your previous pendingintent'll be able to launch it with your new extras even if they is not explicitly given to IT.
This simple explanation, that is, when existing, the original cancellation, and then create a new, in the Alarmmanager service, modify an alarm, with the more stupid method, first cancel, and then re-register, in fact, add this parameter on the line.
Note that this only updates extra data, does not modify other content, cannot be new to a Intent , and if your Intent need to pass the ID or other data, be sure to use this flags or flag_cancel_current , never received the ID, Check it out for a while, that's why:-(
Lag_no_create:if The described pendingintent does not already exist, then simply return null Instead of creating it.
Lag_cancel_current:if the described pendingintent already exists, the current one is canceled before generating a new O Ne. Retrieve a new pendingintent if you have only changing the extra data in the Intent ; By canceling the previous pending Intent , this ensures then only entities given the new Data would be able to launch it. If This assurance was not a issue, consider flag_update_current.
most commonly used in the 4 flags above is flag_update_current, because the description of the Intent You need to use this flag to update your description (specifically update extra data), otherwise the component will be extras for the first time when the next event occurs or time arrives Intent Extras, use flag_cancel_current can also do update extras, but first to remove the front extras, in addition Flag_cancel_current and Flag_update_ The difference between current is whether it is possible to new a Intent , flag_cancel_current to new one Intent , and flag_update_current cannot, only use Intent for the first time.
There is also a problem is how to distinguish between pendingintent, the main cancellation of the time to use, requestcode can be differentiated, but the system is still based on Intent action to distinguish, if Intent set the Class,classdata, cancel when the Intent must be set to the same, or cancel the cancellation may occur after the cancellation, Alarm will also ring the problem,pendingintent with a lot of places , like notifications, Alarmmanager, etc. will be used ...
The following is a reference article:
Http://yichen914.spaces.live.com/blog/cns!723590D920FAF62B!703.entry
http://hi.chinaunix.net/?uid-21747227-action-viewspace-itemid-48368
http://jimmymouse.javaeye.com/blog/760505
This article is from the "Beyond the Dream" blog, please be sure to keep this source http://lbrant.blog.51cto.com/2400264/424154
--------------------------------------------------------------------------------------------------------------- ---------------------------
Pendingintent Simple Comprehension
Pendingintent a literal meaning: waiting, not determinedIntent。
To get a Pendingintent object, use the static method of the method classgetActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), getService(Context, int, Intent, int) 分别对应着Intent的3个行为,跳转到一个activity组件、打开一个广播组件和打开一个服务组件。
参数有4个,比较重要的事第三个和第一个,其次是第四个和第二个。可以看到,要得到这个对象,必须传入一个Intent作为参数,必须有context作为参数。
Pendingintent is a special kind ofIntent。 The main difference isIntentExecution is immediate, and the execution of the pendingintent is not immediate. The action performed by Pendingintent is essentially a parameter passed inIntentoperation, but the purpose of using pendingintent is to include theIntentThe execution of the operation is required to meet certain conditions.
Main use places and examples: noticenotificatio Send, Short messageSmsmanager send and alarm alarmmanager execution and so on.
Android the status bar notification (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 Get Notification Manager Notificationmanager , it is also a system service
2 Create a notification Notification Notification = new Notification (icon, null, when);
3 set parameters for new notifications ( such as sound, vibration, flashing lights )
4 add a new notification 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 argument is a short tip title , The third one is the notification time
notification.defaults = notification.default_sound;// Make default Sound
notification.flags |= notification.flag_auto_cancel;// automatically clear notifications when you click Notifications
Intent openintent = new Intent(this, otheractivity.class);
pendingintent contentintent = pendingintent.getactivity (this, 0, openintent, 0);// When a message is clicked, it is sent to the system openintent Intent
Notification.setlatesteventinfo (This, " title ", " I am content ", contentintent);
mnotificationmanager.notify (0, notification);// The first parameter is a custom notification unique identifier
the point is Setlatesteventinfo () the last parameter of the Method!!! It is a pendingintent!!!!!!!!!
This is used in the pendingintent (Pend intention is to be determined, indeterminate meaning )
pendingintentcan be seen as theIntentthe packaging. pendingintentThe main possession of the information is that it is packagedIntentand the currentApplicationof theContext. Just becausependingintentis saved with the currentApplicationof theContext, giving it a way to carry out a program.Intentthe ability to do so, even if the currentApplicationno longer exists, it can be passed through existence.pendingintentin theContextstill executeIntent.
pendingintent A good example of this:
Smsmanager methods for sending text messages:
Sendtextmessage (destinationaddress, scaddress, text, sentintent, deliveryintent);
First parameter: destinationaddress phone number of each other
a second parameter: scaddress SMS Center number General set to empty
A third parameter: text SMS Content
fourth parameter: sentintent sim card, or the network is interrupted, you can use this itent
fifth parameter: deliveryintent When the text message is sent to the recipient, you receive this deliveryintent
that is, in " SMS sent successfully " and " The other receives this text message " sentintent deliveryintent these two Intent Intent
As the above two examples can be understood,pendingintent is a Intentthat can be executed under certain conditions, compared to Intent The advantage is that you carry a context object so that he doesn't have to rely on an activity to exist.
About Pendingintent in Android