【发布时间】:2012-07-18 01:38:22
【问题描述】:
这似乎是一个常见问题,我已经找到了所有相关问题: Activity isn't picking up new intent,Why extra data (integer) is not sent in android notification intent?,Notification passes old Intent Extras,Can't put extras for an intent in notification,android pending intent notification problem;但仍然无法弄清楚。
问题是一样的。我设置了一个带有 PendingIntent 的通知,其中包含一些额外的信息,但我没有在另一边得到它。
这是生成通知的代码:
Notification notification = new Notification(R.drawable.icon, getResources().getString(R.string.notification_ticker), System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE;
Intent start_test = new Intent(this, MyActivity.class);
start_test.putExtra("start_test", true);
start_test.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pi = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), start_test, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(this, getResources().getString(R.string.notification_title), getResources().getString(R.string.notification_content, expired), pi);
nm.notify(NOTIFICATION_ID, notification);
另一方面:
boolean start_test=getIntent().getBooleanExtra("start_test", false);
捆绑包实际上不存在(getExtras() 返回 null)。
我为PendingIntent.getActivity(FLAG_UPDATE_CURRENT、FLAG_CANCEL_CURRENT、FLAG_ONE_SHOT)尝试了不同的标志,但都没有帮助。
如代码所示,我使用 getCurrentMillis 来确保 requestID 发生变化......
还发现在 MyActivity 中,onCreate 和 onNewIntent 没有被调用。只有onResume 是。即使设置了FLAG_ACTIVITY_NEW_TASK...?
我错过了一些非常简单的东西吗?
【问题讨论】:
-
如果您只想添加一个额外内容,请忘记使用
Bundle。只需使用new_intent.putExtra("start_test", true),然后在另一端使用getIntent.getBooleanExtra("start_test", false) -
是的,我也尝试过,它没有改变任何东西......仍然没有在另一边得到它
-
发生这种情况时,您的应用程序是否正在运行(或在后台)?
MyActivity是否在活动堆栈的顶部? -
@DavidWasser,是的,MyActivity 仍在后台运行(尚未调用 onDestroy)。不确定它是否在活动堆栈的顶部或不在该任务中。
-
new_intent.putExtra("start_test", true) 然后在另一端使用 getIntent.getBooleanExtra("start_test", false) 不起作用
标签: android android-intent android-notifications