2

以下代码显示一个通知并发送 int 数据,但在其他活动中getExtras()返回null。为什么?

int notificationID = 1;
NotificationManager nm = (NotificationManager) getSystemService(getApplicationContext().NOTIFICATION_SERVICE);


Intent i = new Intent(getApplicationContext(), DownlodResult.class);
i.putExtra("notificationID", 1);


PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, i, 0);


CharSequence tickerText = "There are updates !";
long when = System.currentTimeMillis();
int icon = R.drawable.ic_launcher;
Notification notification = new Notification(icon,tickerText,when);
CharSequence contentTitle = "There are updates";
CharSequence contentText = "Please click here to view it";
notification.setLatestEventInfo(getApplicationContext(), contentTitle, contentText, contentIntent);



notification.vibrate = new long[] { 100, 250, 100, 500}; // Needs vibrate permissions
nm.notify(notificationID, notification);
4

1 回答 1

15

可能 PendingIntent 已经存在并且没有被创建,而是被重用。尝试使用PendingIntent.FLAG_UPDATE_CURRENT标志创建它:

PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, i, PendingIntent.FLAG_UPDATE_CURRENT);

请参阅https://stackoverflow.com/a/9330144/530804和关于该问题的其他答案。

于 2012-07-13T18:12:40.783 回答