PendingIntent

本文详细介绍了Android中PendingIntent的概念及其与Intent的区别,通过实例说明了如何使用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.

PendingIntentAndroid 中的一个重要概念,它是一个对 Intent 的描述,可将该描述交给其他程序,使其他程序在未来的某个时间执行安排好的操作。与普通 Intent 不同,普通 Intent 表示当前想要执行的操作,可在 Activity 中立即执行;而 PendingIntentIntent 进行了包装,不一定马上执行,可传递给其他 Activity 或应用程序,获取到 PendingIntent 的应用程序能根据其中的 Intent 了解发出者的意图,并选择拦截、继续传递或执行 [^2]。 使用 PendingIntent 时,需使用其静态方法(如 `getActivity()`、`getService()`、`getBroadcast()`)创建对象,这些方法会返回一个 PendingIntent 对象,用于描述将来要执行的操作 [^1]。同时,创建 PendingIntent 时可设置不同的 Flags 类型,具体如下: - `FLAG_ONE_SHOT`:得到的 PendingIntent 只能使用一次,第二次使用时会报错。 - `FLAG_NO_CREATE`:当 PendingIntent 不存在时,不创建,返回 null。 - `FLAG_CANCEL_CURRENT`:每次都创建一个新的 PendingIntent。 - `FLAG_UPDATE_CURRENT`:不存在时就创建,创建好后一直使用,每次使用时都会更新 PendingIntent 的数据(使用较多) [^3]。 以下是 PendingIntent 在不同场景下的使用示例: #### 在 AlarmManager 中的使用 ```java import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; // ... Intent intent = new Intent("action", null, context, serviceClass); PendingIntent pi = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); manager.set(AlarmManager.RTC_WAKEUP, milis, pi); ``` #### 在 NotificationManager 中的使用 ```java import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; // ... Intent intent = new Intent(); intent.setAction("myaction"); PendingIntent pi = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification n = new Notification(); n.icon = R.drawable.ic_launcher; n.when = System.currentTimeMillis(); n.setLatestEventInfo(this, "this is title", "this is a message", pi); nm.notify(0, n); ``` PendingIntent 的应用场景广泛,常见的有: - **定时任务**:结合 `AlarmManager` 实现定时提醒、定时执行服务等功能。 - **通知栏消息**:在通知栏消息中设置点击后的操作,如点击通知栏消息打开特定的 Activity 或执行广播等。 - **远程服务调用**:将 PendingIntent 传递给其他应用或系统服务,让其在合适的时机执行特定操作。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值