PendingIntent
2015-07-20 14:15
323 查看
PendingIntent和Intent的区别pendingIntent翻译过来是未决定的、即将发生的Intent。与Intent的区别在于Intent的执行是立刻的,pendingIntent的执行是迟缓的。最大的区别是intent 随所在的activity 消失而消失。 而PendingIntent中保存有一个Context,就算在执行时当前Application已经不存在了,也能通过存在PendingIntent里的Context去执行Intent。PendingIntent的获取
PendingIntent.getActivity(context, requestCode, intent, flags); 在满足条件会打开Activity PendingIntent.getBroadcast(context, requestCode, intent, flags); 在满足条件会发送广播 PendingIntent.getService(context, requestCode, intent, flags); 在满足条件会打开服务分别对应着Intent的3个行为,跳转到一个activity组件、打开一个广播组件和打开一个服务组件。其中获取PendingIntent有4个参数:context: 上下文requestCode: 用于标识当前的PendingIntent。不同的requestCode代表不同的PenddingIntent。http://www.cnblogs.com/fengzhblog/archive/2013/07/21/3203363.html证实了这一点intent :所包裹的intent,可以存放extra。Flag:pendingIntent的行为描述:FLAG_UPDATE_CURRENT:Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent 指示如果两个PendingIntent一样的话,只保持第一个,第二个PendingIntent不会生效,仅仅会更新第一个PendingIntent的extra值。FLAG_ONE_SHOT:Flag indicating that this PendingIntent can be used only once. 指示当前Pending只有第一次有效。FLAG_CANCEL_CURRENT:Flag indicating that if the described PendingIntent already exists, the current one should be canceled before generating a new one. 指示如果描述的PendingIntent已经存在,则取消这个存在的,然后生成一个新的。与FLAG_UPDATE_CURRENT相反FLAG_NO_CREATE:Flag indicating that if the described PendingIntent does not already exist, then simply return null instead of creating it. 如果要创建的PendingIntent,在系统中已经不存在(被消耗了或压根没生成过),就返回null而不再创建这个PendingIntent。关于怎么判定两个PedningIntent是否一样:需要判断两个参数,requestCode 和 Intent。而判断Intent是否相等,需要参考action、category、data、flags、cmp等是否相等。而intent的extra值并不会影响Intent是否相等。也就是前几个参数都相等时,才会判断PendingIntent相等。PendingIntent在闹钟中的参数设置设置闹钟的PendingIntent需要从广播获取pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, flags);alarm.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, triggerAtMillis, pendingIntent);alarm.cancel(pendingIntent);当FLAG设为FLAG_UPDATE_CURRENT时,只要两个PendingIntent相等,后面再用这个PendingIntent去设置闹钟,则后一个PedningIntent的extra值会替换掉前一个PendingIntent的前一个值。如果闹钟时间不一样,就相当于开了两个闹钟。其他的FLAG可以参考上面的FLAG 参数介绍cancle为取消闹钟,同理,只有当取消闹钟的PendingIntent和设置闹钟的PendingIntent一样时,才能取消成功。不过如果设置闹钟的PendingIntent的FLAG设为FLAG_ONE_SHOT时,则cancle方法都不会起作用。PendingIntent在发短信中的应用
private final static String SEND_ACTION = "send";
private final static String DELIVERED_ACTION = "delivered";
private void sendSms(String receiver, String text) {
SmsManager s = SmsManager.getDefault();
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SEND_ACTION),
PendingIntent.FLAG_CANCEL_CURRENT);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED_ACTION),
PendingIntent.FLAG_CANCEL_CURRENT);
// 发送完成
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "Send Success!", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Send Failed because generic failure cause.",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "Send Failed because service is currently unavailable.",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Send Failed because no pdu provided.", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Send Failed because radio was explicitly turned off.",
Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getBaseContext(), "Send Failed.", Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SEND_ACTION));
// 对方接受完成
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "Delivered Success!", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getBaseContext(), "Delivered Failed!", Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED_ACTION));
// 发送短信,sentPI和deliveredPI将分别在短信发送成功和对方接受成功时被广播
s.sendTextMessage(receiver, null, text, sentPI, deliveredPI);
}
来源: <http://blog.csdn.net/yuzhiboyi/article/details/8484771>从代码中可以看出,在发短信过程中,PendingIntent在发送成功和送达时,会发送两个广播,我们可以在代码中注册这个两个广播,来监听发送成功和送达的状态。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories