IntentPendingIntent的关系,初学的时候很迷惑,用PendingIntent的时候,还会出现奇怪的问题,比如无法传递数据,无法更新数据,所以我集众家之长,加上我个人的一些实践,总结如下,希望能给你一些帮助。

首先看官方解释:An Intent is something that is used right now; a PendingIntent is something that may create an Intent in the future. You will use a PendingIntent with Notifications, AlarmManager, etc.

Intent大家都很熟悉了,就是一个意图,这个意图表明要启动哪个Activity,Service,PendingIntent可以看作是对Intent的进一步封装,它是对Intent的描述,我们可以把这个描述交给别的程序,别的程序根据这个描述在后面的时间做你安排做的事情,下面是一个发送SMS短信的例子:

  1. String msg ="你好";
  2. String number = "135****6784";
  3. SmsManager sms = SmsManager.getDefault();
  4. PendingIntent pi = PendingIntent.getBroadcast(SmsActivity.this,0,new Intent(XXX),0);
  5. sms.sendTextMessage(number, null, msg, pi, null);
  6. Toast.makeText(SmsActivity.this,"发送成功",Toast.LENGHT_LONG).show();

方法SmsManager.sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent):

  1. PendingIntent sentIntent:当短信发出时,成功的话sendIntent会把其内部的描述的intent广播出去,当然失败之后会产生错误代码,并通过 android.app.PendingIntent.OnFinished进行回调("Callback");
  2. PendingIntent deliveryIntent:是当消息已经传递给收信人后所进行的Intent广播;

如果你的BroadcastReveiver注册接收相应的消息,你就会收到相应的Intent,这时候就可以根据Intent的Action,执行相应的动作,这就是上面说的in the future的含义;

有三个静态方法可以获得PendingIntent实例:

  • public static PendingIntent getBroadcast(Context context, int requestCode, Intent intent, int flags)
  • public static PendingIntent getActivity(Context context, int requestCode, Intent intent, int flags)
  • public static PendingIntent getService(Context context, int requestCode, Intent intent, int flags)

flags参数有三个,我觉得英文更好理解:

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.

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.
这个简单解释一下,就是当存在时,先把原来的取消,然后创建一个新的,在AlarmManager服务时,修改一个闹铃,用的比较笨的的方法,先取消,然后重新注册,其实加上这个参数就行了。

要注意的是,这个只更新extra data,不会修改其他内容,不能new一个Intent,还有就是如果你的Intent中需要传递Id或者其他数据,一定要用这个flags或者FLAG_CANCEL_CURRENT,曾经一直接收不到Id,查了半天原来是这个原因 :-(

LAG_NO_CREATE:if the described PendingIntent does not already exist, then simply return null instead of creating it.

LAG_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.

上面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等都会用到。。。

原文地址:http://hi.baidu.com/lphack/blog/item/161c7fd7b9684d3d07088ba0.html

转载于:https://www.cnblogs.com/yingql/archive/2011/06/05/2073337.html

【转】PendingIntent的总结相关推荐

  1. android Intent PendingIntent的区别

    含义:intent英文意思是意图,pending表示即将发生或来临的事情.  PendingIntent这个类用于处理即将发生的事情.比如在通知Notification中用于跳转页面,但不是马上跳转. ...

  2. Intent和PendingIntent的区别

    intent英文意思是意图,pending表示即将发生或来临的事情.  PendingIntent这个类用于处理即将发生的事情.比如在通知Notification中用于跳转页面,但不是马上跳转. In ...

  3. 发通知 PendingIntent 中Intent 内容没有更新

    2019独角兽企业重金招聘Python工程师标准>>> 当我们把Activity 启动模式设置为 singleTask 之后 当我们下次 再去 用Intent 启动 这个 Activ ...

  4. 什么是Android PendingIntent?

    我是Android的新手. 我阅读了Android文档,但我仍需要进一步澄清. 任何人都可以告诉我PendingIntent究竟是什么? #1楼 待定意图是一种意图,将在未来的某个时刻开始. 正常意图 ...

  5. Android开发--Notification和PendingIntent

    手机上方的状态栏用于显示通知消息,实现的方法很简单,只需要记住五个步骤即可(步骤写在源代码中的注释中).pendingIntent意思是延期执行的Intent,一般用于Notification中,下面 ...

  6. AlarmManager与PendingIntent的联合使用(二)

    PendingIntent.getService配合AlarmManager,自定义个类继承Service: package com.example.alarmmanager;import andro ...

  7. AlarmManager与PendingIntent的联合使用(一)

    AlarmManager与PendingInteng.getBroadcast及getActivity: package com.example.alarmmanager;import android ...

  8. PendingIntent详解

    PendingIntent描述了一个Intent和目标action.这个类的实例是用getActivity(Context,int,Intent,int)方法和getBroadcast(Context ...

  9. android PendingIntent参数详细解析

    PendingIntent介绍 PendingIntent可以看作是对Intent的一个封装,但它不是立刻执行某个行为,而是满足某些条件或触发某些事件后才执行指定的行为. PendingIntent的 ...

最新文章

  1. spacevim 添加自动折行
  2. linux less命令简介
  3. HDU 4417 Super Mario(莫队 + 树状数组 + 离散化)
  4. Redis简单案例(三) 连续登陆活动的简单实现
  5. 用python写论文_[文章] 编写高质量 Python 的 6 个技巧
  6. 7-1 字符串的冒泡排序 (20 分)
  7. Hawkeye - The .Net Runtime Object Editor
  8. android计算器实现sin功能,简单实现Android计算器功能
  9. 苹果开发者账号绑定设备已满
  10. 对移动社交类产品的追问
  11. MIMO技术杂谈(一):鱼与熊掌能否兼得?--浅谈分集与复用的权衡
  12. 24段魔尺拼图指南_拼图项目动手指南
  13. 微信小程序生命周期(onLoad,onHide,onShow,onReady,onUnload)
  14. 零伽壹链改案例:区块链赋能供应链应用 新电商逆袭成长
  15. 基于改进型生成对抗网络生成异构故障样本的方法
  16. 解决 error: failed to push some refs to ‘https://github.com/mxp520/test-spring-cloud-base.git‘
  17. Hadoop全分布式部署 - CentOS(结尾附视频)
  18. 高逼格PPT各种资源站点汇总
  19. 项目管理(PJM)与Sage X3 ERP的集成应用
  20. 37-博客网站数据库-博文分类信息数据操作(一)

热门文章

  1. iptables防火墙策略
  2. CSP 201812-1 小明上学 Python实现+详解
  3. 参加Python培训后能干嘛
  4. Python入门学习方法有哪些?
  5. 关于高速光耦6n137的使用总结_光耦使用中的那点事1
  6. 2018.10.22-dtoi1443奶牛逃亡(cowrun)
  7. Mysql技术内幕——InnoDB存储引擎
  8. java web项目流程小结
  9. 阿里巴巴开源技术汇总:115个软件(一)
  10. SystemCenter2012SP1实践(15)共享库服务器和ISO