我在我的 Android 应用程序中使用通知。每当我收到通知时,我都会尝试使用 notification.number 增加未读通知的数量,如下面的代码所示。
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon,
"A new notification", System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.number += 1;
Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "This is the title",
"This is the text", activity);
notificationManager.notify(0, notification);
但是,无论我收到多少通知,计数器都不会递增。它总是只显示 1。我不明白这里出了什么问题。谁能帮帮我...?
最佳答案
问题是,您正在创建一个新的通知对象。只需创建一次,检查它是否存在于字段中
public class main extends Activity {
Notification notification=null;
//....
if (this.notification == null)
this.notification = new Notification(R.drawable.icon,
"A new notification", System.currentTimeMillis());
this.notification.flags |= Notification.FLAG_AUTO_CANCEL;
this.notification.number += 1;
关于android - 在 android 中递增 notification.number,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9965967/