PendingIntent

PendingIntent跟Intent类似,字面意思可以看作“意图”,从功能上看基本相同,他们都可以用来启动活动、服务以及发送广播等;不同点在于,Intent倾向于及时执行某个动作,而PendingIntent比Intent多了个等待的过程,它更倾向于在某个合适的实际实行指定的动作,因此可以将它简单理解成延迟执行的Intent。

根据google的API:

A description of an Intent and target action to perform with it. Instances of this class are created with getActivity(Context, int, Intent, int), getActivities(Context, int, Intent[], int), getBroadcast(Context, int, Intent, int), and getService(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.

PendingIntent的使用方法是使用上述三个静态方法获取实例,可以根据自己的需求来选择是使用getActivity(Context, int, Intent, int)getActivities(Context, int, Intent[], int)getBroadcast(Context, int, Intent, int), 或者getService(Context, int, Intent, int)方法。返回的对象会传递给其他的应用让它们在一段时间后执行你所描述的行为。观察可以发现这几个方法接收的参数完全相同:第一个参数Context;第二个参数般置0;第三个参数是一个Intent对象,用于构建PendingIntent的“意图”;最后一个参数用于确定PendingIntent的行为,查阅文档可知一般有4种值可选:FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT, FLAG_IMMUTABLE,一般也是置0。

getBroadcast

added in API level 1
PendingIntent getBroadcast (Context context, 
                int requestCode, 
                Intent intent, 
                int flags)

Retrieve a PendingIntent that will perform a broadcast, like calling Context.sendBroadcast().

For security reasons, the Intent you supply here should almost always be an explicit intent, that is specify an explicit component to be delivered to through Intent.setClass

Parameters
contextContext: The Context in which this PendingIntent should perform the broadcast.
requestCodeint: Private request code for the sender
intentIntent: The Intent to be broadcast.
flagsint: May be FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT, FLAG_IMMUTABLE or any of the flags as supported by Intent.fillIn() to control which unspecified parts of the intent that can be supplied when the actual send happens.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值