PendingIntent是Intent的封装,不是立刻执行某个行为,而是满足某些条件或触发某些事件后才执行指定的行为
实例获取一般为下列5个用法
getActivity()
getActivities()
getBroadcast()
getService()
getForegroundService()
AlarmManager(闹钟类型int,闹钟首次执行时间long,响应闹钟的动作PendingIntent);
PendingIntent pi: 绑定了闹钟的执行动作,比如发送一个广播、给出提示等等。PendingIntent是Intent的封装类。
如果是通过启动服务来实现闹钟提示的话,PendingIntent对象的获取就应该采用PendingIntent.getService(Context c,int i,Intent intent,int j) 方法;
如果是通过广播来实现闹钟提示的话,PendingIntent对象的获取就应该采用 PendingIntent.getBroadcast(Context c,int i,Intent intent,int j)方法;
如果是采用Activity的方式来实现闹钟提示的话,PendingIntent对象的获取就应该采用 PendingIntent.getActivity(Context c,int i,Intent intent,int j)方法。
如果这三种方法错用了的话,虽然不会报错,但是看不到闹钟提示效果
Context, requestCode, Intent, flags,
它们的参数都相同,都是四个:Context, requestCode, Intent, flags分别对应上下文对象、请求码、请求意图用以指明启动类及数据传递、关键标志位。
前面三个参数共同标志一个行为的唯一性,而第四个参数flags:
FLAG_CANCEL_CURRENT:如果当前系统中已经存在一个相同的PendingIntent对象,那么就将先将已有的PendingIntent取消,然后重新生成一个PendingIntent对象。
FLAG_NO_CREATE:如果当前系统中不存在相同的PendingIntent对象,系统将不会创建该PendingIntent对象而是直接返回null,如果之前设置过,这次就能获取到。
FLAG_ONE_SHOT:该PendingIntent只作用一次。在该PendingIntent对象通过send()方法触发过后,PendingIntent将自动调用cancel()进行销毁,
那么如果你再调用send()方法的话,系统将会返回一个SendIntentException。
FLAG_UPDATE_CURRENT:如果系统中有一个和你描述的PendingIntent对等的PendingInent,那么系统将使用该PendingIntent对象,
但是会使用新的Intent来更新之前PendingIntent中的Intent对象数据,例如更新Intent中的Extras