PendingIntent学习笔记

2016-08-11  本文已影响386人  CalvinNing

写在前面的话####

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


  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


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.
上一篇 下一篇

猜你喜欢

热点阅读