intent 英文意思是意图,pending 表示即将发生或来临的事情。
PendingIntent 这个类用于处理即将发生的事情,比如在通知Notification中用于跳转页面,但不是马上跳转。
Intent 是及时启动,intent 随所在的activity 消失而消失。
PendingIntent 可以看作是对intent的包装,通常通过getActivity,getBroadcast,getService来得到pendingintent的实例,当前activity并不能马上启动它所包含的intent,而是在外部执行 pendingintent 时,调用intent的。正由于pendingintent中保存有当前App的Context,使它赋予外部App一种能力,使得外部App可以如同当前App一样的执行pendingintent里的 Intent, 就算在执行时当前App已经不存在了,也能通过存在pendingintent里的Context照样执行Intent。另外还可以处理intent执行后的操作。PendingIntent常和alermanger 和notificationmanager一起使用。
Intent一般是用作Activity、Service、BroadcastReceiver之间传递数据;而Pendingintent一般用在 Notification上,可以理解为延迟执行的intent,PendingIntent是对Intent一个包装。
PendingIntent pIntent = PendingIntent.getActivity(this, 0, new Intent(this, NotifyActivity.class), 0);
详见代码:
private void showNotify(){
PendingIntent pIntent = PendingIntent.getActivity(this, 0, new Intent(this, NotifyActivity.class), 0); // pendingIntent
Notification notify = new Notification();
notify.icon = R.drawable.ic_launcher;
notify.tickerText = "您有一条新消息";
notify.defaults = Notification.DEFAULT_SOUND;
notify.when = System.currentTimeMillis();
notify.vibrate = new long[]{0, 50, 100, 150};
notify.contentIntent = pIntent;
notify.setLatestEventInfo(this, "通知", "http://blog.csdn.net/sunboy_2050", pIntent); // context, title, msg, pendingIntent
NotificationManager manager = (NotificationManager)getSystemService(this.NOTIFICATION_SERVICE);
manager.notify(0, notify);
}
通知结果:

GSM网络中android发送短信示例
private void showNotify222(){
String phone = "13212341234";
String msg = "http://blog.csdn.net/sunboy_2050";
PendingIntent pIntent = PendingIntent.getBroadcast(this, 0, new Intent("com.homer.pendingintent.pendingbroadcast"), 0); // pendingIntent
SmsManager manager = SmsManager.getDefault();
if(msg.length() > 70) { // split msg length
List<String> msgList = manager.divideMessage(msg);
for (String msg2 : msgList) {
manager.sendTextMessage(phone, null, msg2, pIntent, null); // destinationAddress, scAddress, text, sentIntent, deliveryIntent
}
} else {
manager.sendTextMessage(phone, null, msg, pIntent, null);
}
}
AndroidManifest.xml 中 Application 节点内注册广播:
<receiver android:name=".PendingReceiver">
<intent-filter>
<action android:name="com.homer.pendingintent.pendingbroadcast" />
</intent-filter>
</receiver>
PendingReceiver 类 主要处理
PendingIntent广播的事情,即发送短信成功后的提醒,实现如下:
public class PendingReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("com.homer.pendingintent.pendingbroadcast")) {
Toast.makeText(context, "Test PendingIntent Success", Toast.LENGTH_LONG).show();
}
}
}
AndroidManifest.xml 根节点下,添加短信发送权限:
<uses-permission android:name="android.permission.SEND_SMS" />
运行结果:

代码下载
代码解释:
PendingIntent 是一个Intent的描述(封装),可以把这个描述交给别的程序,别的程序根据这个描述在后面的时间做自己安排做的事情 (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),就相当于PendingIntent代表了Intent。
本例中别的程序就是发送短信的程序,短信发送成功后要把intent广播出去
SmsManager.sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)
1)PendingIntent sentIntent:当短信发出时,成功的话sendIntent会把其内部的描述的intent广播出去,否则产生错误代码并通过android.app.PendingIntent.OnFinished进行回调,这个参数最好不为空,否则会存在资源浪费的潜在问题;
2)PendingIntent deliveryIntent:是当消息已经传递给收信人后所进行的PendingIntent广播。
如果BroadcastReveiver注册接收相应的消息,你就会收到相应的Intent,这时候就可以根据Intent的Action,执行相应的动作,这就是上面说的in the future的含义;
可以获得PendingIntent实例的静态方法:
-
public static PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags)
- public static PendingIntentgetActivities(Context context, int requestCode, Intent[] intents, int flags)
-
public static PendingIntent getService(Context context, int requestCode, Intent intent, int flags)
- public static PendingIntentgetBroadcast(Context context, int requestCode, Intent intent, int flags)
flags参数有四个:
1) FLAG_ONE_SHOT :this PendingIntent can only be used once. If set, after send() is called on it, it will be automatically canceled for you and any future attempt to send through it will fail.
2)FLAG_UPDATE_CURRENT :if the described PendingIntent already exists, then keep it but its replace its extra data with what is in this new Intent. This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.
3)FLAG_NO_CREATE:if the described PendingIntent does not already exist, then simply return null instead of creating it.
4)FLAG_CANCEL_CURRENT:if the described PendingIntent already exists, the current one is canceled before generating a new one. You can use this to retrieve a new PendingIntent when you are only changing the extra data in the Intent; by canceling the previous pending intent, this ensures that only entities given the new data will be able to launch it. If this assurance is not an issue, consider FLAG_UPDATE_CURRENT.
FLAG_UPDATE_CURRENT这个简单解释一下,就是当存在时,先把原来的取消,然后创建一个新的,在AlarmManager服务时修改一个闹铃,用的比较笨的的方法,先取消然后重新注册,其实加上这个参数就行了。要注意的是,这个只更新extra data,不会修改其他内容,不能new一个Intent,还有就是如果你的Intent中需要传递Id或者其他数据,一定要用这个flags或者FLAG_CANCEL_CURRENT,曾经一直接收不到Id,查了半天原来是这个原因
上面4个flag中最经常使用的是FLAG_UPDATE_CURRENT,因为Intent有更新的时候需要用到这个flag去更新你的描述(确切的说是更新extra data),否则组件在下次事件发生或时间到达的时候extras永远是第一次Intent的extras,使用FLAG_CANCEL_CURRENT也能做到更新extras,只不过是先把前面的extras清除,另外FLAG_CANCEL_CURRENT和FLAG_UPDATE_CURRENT的区别在于能否新new一个Intent,FLAG_CANCEL_CURRENT能够新new一个Intent,而FLAG_UPDATE_CURRENT则不能,只能使用第一次的Intent。
还有一个问题就是怎么区分PendingIntent,主要是取消的时候要用到requestCode区分,但系统还是根据Intent的Action去区分的,如果Intent设置了Class,classData,取消的时候Intent一定要设置要相同的,不然取消不掉就可能出现取消后Alarm还会响的问题,PendingIntent用的地方蛮多的,像 Notifications, AlarmManager等都会用到。。。
查看PendingIntent 类可以看到许多的Send函数,就是PendingIntent在进行被赋予的相关的操作。
参考推荐:
PendingIntent(Android Developer)
PendingIntent与Intent区别
android发送短信
分享到:
相关推荐
简单的总结了Intent和PendtingIntent的区别,经常与alermanger 和notificationmanager一起使用。
在Android开发中,Intent和PendingIntent是两个非常重要的概念,它们在组件间的通信中起到关键作用。Intent可以理解为一种消息传递对象,用于在不同组件之间传递行为和数据,而PendingIntent则是Intent的一种封装,...
当创建PendingIntent时,会根据提供的Intent和Flag来决定哪些应用可以触发这个Intent,以及如何触发。这种设计使得我们可以放心地将PendingIntent传递给第三方应用,如通知系统或闹钟服务。 PendingIntent的创建...
Notification 和 PendingIntent 的使用 Notification 是 Android 系统中的一种机制,用于在系统状态栏中显示通知信息,通常用于提醒用户某些事件的发生。PendingIntent 则是 Android 中的一种机制,用于在特定的...
在Android开发中,Intent和PendingIntent是两个非常关键的概念,它们在应用程序的组件间通信中起着重要作用。Intent主要用于启动或传递数据给另一个组件,如Activity、Service或BroadcastReceiver,而PendingIntent...
3. IntentFlag:在创建PendingIntent时,可以通过setFlags()方法设置Intent标志,比如FLAG_CANCEL_CURRENT和FLAG_UPDATE_CURRENT,它们会影响PendingIntent的行为,如替换已存在的Intent或取消当前PendingIntent并...
当创建`PendingIntent`时,需要提供一个Intent实例,这个Intent包含了操作的目标(如ComponentName)、数据(Uri、Extras等)和操作类型(ACTION_VIEW、ACTION_SEND等)。 3. **唯一标识符**:`PendingIntent`通过`...
1. **创建PendingIntent**: 应用通过Intent和Flags参数创建PendingIntent实例。 2. **传递给外部组件**: 将PendingIntent传递给其他应用或系统服务,如NotificationManager。 3. **外部组件请求执行**: 当外部组件...
2. **获取PendingIntent**:然后,使用PendingIntent的静态工厂方法,如getActivity()、getService()或getBroadcast(),并传入上下文(Context)、Intent、请求码(int)和标志(int)。请求码和标志用于区分不同的...
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); // 将PendingIntent添加到通知中 buildersetContentIntent(contentIntent); // 发送通知 NotificationManager ...
总之,PendingIntent是Android系统中一个强大的工具,它使得Intent的操作可以跨越时间和空间,为应用程序的交互和扩展提供了极大的便利。理解和熟练运用PendingIntent,对于开发高质量的Android应用至关重要。
本实验将深入探讨Android Studio中Intent的使用,帮助你更好地理解如何在不同的Activity之间跳转和传递信息。 首先,让我们了解Intent的基本概念。Intent分为两种类型:显式Intent和隐式Intent。显式Intent用于启动...
`AlarmManager`和`PendingIntent`是Android系统提供的两个关键组件,用于实现这样的定时提醒功能。下面将详细阐述这两个组件的工作原理及其结合使用的方式。 `AlarmManager`是Android系统中的一个服务,它允许应用...
Intent1_Intent.zip中的源码应该包含了关于Intent的实例和使用方法,让我们一起深入探讨Intent在Android应用中的作用、类型、创建与传递、以及常见用法。 1. **Intent的作用** Intent的主要功能是启动一个活动...
归纳了Intent入 的4种形式:Intent转换与复制、 Action/Component/Data注 、PendingIntent误用与 parseUri注 入 归纳了利用自动化的工具具发现这4类形式的方法,通过批 量的扫描,可以轻易发现这些漏洞 在每种都找到了...
总结来说,实现自定义通知声音、通知栏跳转和数据传递的步骤包括:创建自定义音频文件,将其添加到项目资源,使用NotificationCompat.Builder设置声音,构建并设置Intent和PendingIntent以实现点击通知后的行为,...
其中,`"com.example.SMS_SENT"`和`"com.example.SMS_DELIVERED"`是自定义的广播事件,`context`是应用的上下文,`0`是请求码,`FLAG_UPDATE_CURRENT`表示如果存在相同的Intent,更新当前的PendingIntent而不是创建...
3. 异步Intent:通过使用PendingIntent,可以在后台服务或BroadcastReceiver中安全地执行Intent操作。 总之,Intent是Android应用程序组件间通信的桥梁,理解和熟练使用Intent对于Android开发者来说至关重要。通过...
- 使用`PendingIntent`,将Intent包装成可延迟执行的对象,常用于通知、AlarmManager等场景。 - `Intent.createChooser(Intent target, CharSequence title)`:创建一个Intent选择器,让用户从多个可处理相同...