美文网首页
PendingIntent学习笔记

PendingIntent学习笔记

作者: CalvinNing | 来源:发表于2016-08-11 10:09 被阅读386次

写在前面的话####

之前自己在网上查找了几篇相关文章,在此基础上打算写一篇总结性的笔记。写之前心想先看一下Google的API,结果看了之后发现API已经写的非常明确了,所以就干脆把API复制过来了。


  • PendingIntent可以看作是对Intent的一个封装,但它不是立刻执行某个行为,而是满足某些条件或触发某些事件后才执行指定的行为。
  • PendingIntent的获取
  1. PendingIntent getActivity (Context context, int requestCode, Intent intent, int flags, Bundle options)
    Retrieve a PendingIntent that will start a new activity, like calling Context.startActivity(Intent)

Note that the activity will be started outside of the context of an existing activity, so you must use the Intent.FLAG_ACTIVITY_NEW_TASK launch flag in the Intent.

  1. PendingIntent getService (Context context, int requestCode, Intent intent, int flags)
    Retrieve a PendingIntent that will start a service, like calling Context.startService()

The start arguments given to the service will come from the extras of the Intent.

  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


  • PendingIntent的参数说明
    拿第三种方式,广播的形式说明下
    PendingIntent sentIntent = PendingIntent.getBroadcast(this, 0,sIntent, 0);
    第一个参数是上下文.
    第二个参数是每次requestcode不同,就能产生多个Pendingintent.
    第三个参数是用来存储信息.
    第四个参数是对不同操作作标识.

getBroadcast(Context context, int requestCode, Intent intent, int flags)中的flags有几种状态:

  1. FLAG_CANCEL_CURRENT:如果AlarmManager管理的PendingIntent已经存在,那么将会取消当前的PendingIntent,从而创建一个新的PendingIntent.
  2. FLAG_UPDATE_CURRENT:如果AlarmManager管理的PendingIntent已经存在,让新的Intent更新之前Intent对象数据,例如更新Intent中的Extras,另外,我们也可以在PendingIntent的原进程中调用PendingIntent的cancel ()把其从系统中移除掉
  3. FLAG_NO_CREATE:如果AlarmManager管理的PendingIntent已经存在,那么将不进行任何操作,直接返回已经.
  4. FLAG_ONE_SHOT:该PendingIntent只作用一次.在该PendingIntent对象通过send()方法触发过后,PendingIntent将自动调用cancel()进行销毁,那么如果你再调用send()方法的话,系统将会返回一个SendIntentException.

相关文章

网友评论

      本文标题:PendingIntent学习笔记

      本文链接:https://www.haomeiwen.com/subject/fvojsttx.html