您好我想在一个视图中显示所有通知..并想要更新状态栏中的通知数...它更新所有信息但显示数字始终为1 ..请告诉我如何解决它.. 。
@Override
public void onReceive(Context context, Intent intent)
{
//Random randGen = new Random();
//int notify_id = randGen.nextInt();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Activity.NOTIFICATION_SERVICE);
String title = intent.getStringExtra(TableUtils.KEY_TITLE);
String occasion = intent.getStringExtra(TableUtils.KEY_OCCASION);
Notification notification =
new Notification(R.drawable.icon, "Love Cardz" ,
System.currentTimeMillis());
// notification.vibrate = new long[]{100,250,300,330,390,420,500};
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.number+=1;
Intent intent1 = new Intent(context, ThemesBrowserActivity.class);
PendingIntent activity =
PendingIntent.getActivity(context, 1 , intent1,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, occasion, title, activity);
notificationManager.notify(1, notification);
}
答案 0 :(得分:19)
你必须跟踪计数。您可以扩展Application类:
public class AppName extends Application {
private static int pendingNotificationsCount = 0;
@Override
public void onCreate() {
super.onCreate();
}
public static int getPendingNotificationsCount() {
return pendingNotificationsCount;
}
public static void setPendingNotificationsCount(int pendingNotifications) {
pendingNotificationsCount = pendingNotifications;
}
}
你应该修改onReceive:
@Override
public void onReceive(Context context, Intent intent) {
...
int pendingNotificationsCount = AppName.getPendingNotificationsCount() + 1;
AppName.setPendingNotificationsCount(pendingNotificationsCount);
notification.number = pendingNotificationsCount;
...
}
您可以在用户打开通知时重置计数:
AppName.setPendingNotificationsCount(0);
答案 1 :(得分:4)
这是我的代码,它有效。我只测试了旧的Android版本。我怀疑在较新的版本中,“数字”徽章已被隐藏,但我没有机会对其进行测试。
void notifyMsgReceived(String senderName, int count) {
int titleResId;
String expandedText, sender;
// Get the sender for the ticker text
// i.e. "Message from <sender name>"
if (senderName != null && TextUtils.isGraphic(senderName)) {
sender = senderName;
}
else {
// Use "unknown" if the sender is unidentifiable.
sender = getString(R.string.unknown);
}
// Display the first line of the notification:
// 1 new message: call name
// more than 1 new message: <number of messages> + " new messages"
if (count == 1) {
titleResId = R.string.notif_oneMsgReceivedTitle;
expandedText = sender;
}
else {
titleResId = R.string.notif_missedCallsTitle;
expandedText = getString(R.string.notif_messagesReceivedTitle, count);
}
// Create the target intent
final Intent intent = new Intent(this, TargetActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
final PendingIntent pendingIntent =
PendingIntent.getActivity(this, REQUEST_CODE_MSG_RECEIVED, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Build the notification
Notification notif = new Notification(
R.drawable.ic_notif_msg_received, // icon
getString(R.string.notif_msgReceivedTicker, sender), // tickerText
System.currentTimeMillis()); // when
notif.setLatestEventInfo(this, getText(titleResId), expandedText, pendingIntent);
notif.number = count;
notif.flags = Notification.FLAG_AUTO_CANCEL;
// Show the notification
mNotificationMgr.notify(NOTIF_MSG_RECEIVED, notif);
}
稍后更新通知也很容易:您只需要使用新值再次调用该方法。当且仅当创建通知时,该号码将显示在通知图标徽章中。
类似地,如果您将数字设置为小于1的数字,则数字徽章将不会被隐藏(数字将为您)。可能在重新显示之前清除通知可能会修复它。
答案 2 :(得分:0)
你必须跟踪计数。您尝试在notif.number上执行的增量不起作用,因为该状态不可用(即notif.number始终为0,然后将其递增为1)。跟踪应用程序中的某个位置(可能是共享首选项),然后将其增加并存储在那里,然后在构建通知时设置
notif.number = myStoredValueForWhatShouldShowInNumber;
试一试。