几天前,我苦苦寻找一种使用自定义意图来设置闹钟的方法。尽管我得到了明确的答案,即我必须基于某个唯一的ID定制意图,例如setAction()
,但仍然存在一些问题。
我这样定义了一个PendingIntent:
Intent intent = new Intent(this, viewContactQuick.class);
intent.setAction("newmessage"+objContact.getId());//unique per contact
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK ).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP );
intent.putExtra("id", Long.parseLong(objContact.getId()));
intent.putExtra("results", result.toArray());
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, intent, 0);
那么这会被通知管理器使用。
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
// first try to clear any active notification with this contact ID
mNotificationManager.cancel(Integer.parseInt(objContact.getId()));
// then raise a new notification for this contact ID
mNotificationManager.notify(Integer.parseInt(objContact.getId()), notification);
这是实现方式:
- 应用程序为联系人创建一条消息
- 提供一个带有联系人 ID 和消息详细信息的Intent
- 使用该消息触发通知
- 用户在通知上执行操作,应用程序会显示意图传递的消息
问题
同一个联系人可能会收到多条消息。当第二条消息生成时,通知也能正常弹出(消息正常),但是当用户操作通知时,该intent使用旧数据而不是新数据传递,即传递的是之前的消息而非全新的消息。
所以用某种方法使得intent对于每个联系人和每个动作都是唯一的吗?