17

如何每次都创建待处理的意图?目前,我现有的待定意图正在被新的意图所取代。我尝试使用FLAG_ONE_SHOTas well,CANCEL_CURRENT但它没有用。

4

2 回答 2

37

在请求代码中添加一个随机数,如下所示:

Intent intent = new Intent(context, YourClassname.class);
intent.putExtra("some data", "txt");  // for extra data if needed..

Random generator = new Random();

PendingIntent i=PendingIntent.getActivity(context, generator.nextInt(), intent,PendingIntent.FLAG_UPDATE_CURRENT);
于 2013-03-23T15:28:40.483 回答
11

FLAG_CANCEL_CURRENT - 如果描述的 PendingIntent 已经存在,则在生成新的之前取消当前的。

FLAG_NO_CREATE - 如果所描述的 PendingIntent 尚不存在,则只需返回 null 而不是创建它。

FLAG_ONE_SHOT - 这个 PendingIntent 只能使用一次。

FLAG_UPDATE_CURRENT - 如果描述的 PendingIntent 已经存在,则保留它,但用这个新 Intent 中的内容替换其额外数据。

如果您确实需要同时激活多个不同的 PendingIntent 对象(例如用作同时显示的两个通知),那么您将需要确保它们有所不同,以便将它们与不同的待定意图。这可能是 考虑的任何 Intent 属性Intent.filterEquals,或提供给的不同请求代码整数getActivity(Context, int, Intent, int), getActivities(Context, int, Intent[], int), getBroadcast(Context, int, Intent, int), or getService(Context, int, Intent, int).

于 2013-03-08T16:00:44.840 回答