Android PendingIntent使用的特殊情况

来源:互联网 发布:矢量图网上制作软件 编辑:程序博客网 时间:2024/05/29 19:29

PendingIntent

PendingIntent的是代表了将要发生的意图,使用的时候大概涉及到几方面:

  • SMS中在接收到短信发送状态的时候,可以设置一些回调
  • 在警报时可以用到
  • 在使用Notification时可以设置点击了提示消息后的动作,特殊的在Android 21以前notification可以设置一个deleteIntent来设置,消息被删除后的动作。在Android 21以后Android提供了notification的回调函数,这里不阐述。

    PendingIntent的flag:

    • 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。

特殊需求

在默认情况下Android系统会尽量的复用已经存在的PendingIntent,但是当我们不想复用系统中存在的PendingIntent时。我们可以使用getBroadcast(mContext, flagID, intent, PendingIntent.FLAG_UPDATE_CURRENT)的第二个参数使用不同的值就可以区分不同的PendingIntent。

ss

Intent intent = new Intent();        intent.setAction("clear");        intent.putExtra("id",flagID);        PendingIntent pendingIntent= PendingIntent.getBroadcast(mContext, flagID, intent, PendingIntent.FLAG_UPDATE_CURRENT);notify.deleteIntent = pendingIntent;
0 0