短信代码里有用到这个类
下面是从api文档中摘下来两段
A description of an Intent and target action to perform with it. Instances of this class are created withgetActivity(Context, int, Intent, int)
,getActivities(Context, int, Intent[], int)
,getBroadcast(Context, int, Intent, int)
, andgetService(Context, int, Intent, int)
; the returned object can be handed to other applications so that they can perform the action you described on your behalf at a later time.
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 (with the same permissions and identity). As such, you should be careful about how you build the PendingIntent: often, for example, the base Intent you supply will have the component name explicitly set to one of your own components, to ensure it is ultimately sent there and nowhere else.
个人理解:一种预先绑定,延时触发机制。
比如:发送短信sms,当调用了SmsManager中的方法
SmsManager.getDefault().sendTextMessage(**, **, **,sentIntent, deliveryIntent);
发送消息需要反馈消息:发送是否成功,对方是否接受到(消息报告)。如何对这些反馈消息进行相应的处理?这个就是PendingIntent的作用。
如上面这句代码,就是当消息发送状态改变时触发sentIntent动作,当对方接收到短信触发deliveryIntent动作。
下面总结个小图(没有验证底层操作是否如此)。
这样pendingIntent就跟普通的按钮事件没啥区别,只是没有按钮事件那么清楚,当你点击了一个按钮的时候会触发这个按钮绑定的事件。
pendingIntent绑定的是特殊的事件,当得到一定的时机的时候就触发绑定的消息。
两者的区别只是事件的触发条件不一样,一个是按钮点击,一个复杂些