【发布时间】:2011-08-17 20:17:06
【问题描述】:
我正在从具有额外整数意图的服务创建通知。出于某种原因,这并不像看起来那么容易。这就是我正在做的:
Notification notification = new Notification(R.drawable.notification_icon, "Title", 0);
Intent relaunchIntent = new Intent(this, SomeClass.class);
relaunchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
relaunchIntent.putExtra("load", getId());
notification.setLatestEventInfo(this, "Title", "Tap here to open", PendingIntent.getActivity(this, 0, relaunchIntent, 0);
notificationManager.notify(1234, notification);
然后从 SomeClass 中,它看起来像这样......
if(getIntent().getExtras() != null)
load(getIntent().getExtras().getInt("load", -1));
奇怪的是,当我从 getInt() 方法中读取值时,我得到的结果不一致。有时我会得到我在之前的通知中设置的东西的值。例如,当我将额外设置为 4 时,我得到的值为 3。我知道这非常令人困惑和奇怪,这就是为什么我想知道是否有人经历过这样的事情以及您可能采取了什么措施来解决它。谢谢!
【问题讨论】:
-
可能想看看使用
PendingIntent.FLAG_UPDATE_CURRENT来自this answer 的评论。 -
谢谢,这个问题的答案成功了。你应该考虑把它作为答案,我会接受的。
标签: android service notifications android-intent extra