为什么额外的数据(整数)没有在Android的通知发送的意图? [英] Why extra data (integer) is not sent in android notification intent?

查看:134
本文介绍了为什么额外的数据(整数)没有在Android的通知发送的意图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code显示通知和发送int数据,但在其他活动 getExtras()收益。为什么呢?

  INT notificationID = 1;
NotificationManager毫微米=(NotificationManager)getSystemService(getApplicationContext()NOTIFICATION_SERVICE);
意图I =新意图(getApplicationContext(),DownlodResult.class);
i.putExtra(notificationID,1);
的PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(),0,I,0);
CharSequence的tickerText =有更新!
时长= System.currentTimeMillis的();
INT图标= R.drawable.ic_launcher;
通知通知=新的通知(图标,tickerText时);
CharSequence的contentTitle =有更新;
CharSequence的contentText =请点击这里进行查看
notification.setLatestEventInfo(getApplicationContext(),contentTitle,contentText,contentIntent);notification.vibrate =新长[] {100,250,100,500}; //需要震动权限
nm.notify(notificationID,通知);


解决方案

也许存在的PendingIntent已经并没有创建,但重复使用。尝试与 PendingIntent.FLAG_UPDATE_CURRENT 标记创建它:

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

请参阅 http://stackoverflow.com/a/9330144/530804 并就这个问题对方回答。

The following code to display a notification and send int data, but in the other activity getExtras() returns null. Why?

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);

解决方案

Probably the PendingIntent exists already and was not created, but reused. Try to create it with the PendingIntent.FLAG_UPDATE_CURRENT flag:

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

See http://stackoverflow.com/a/9330144/530804 and the other answer on that question.

这篇关于为什么额外的数据(整数)没有在Android的通知发送的意图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆