2010-09-07 37 views
7

我正在使用自定义通知...我如何设置没有通知显示?并列出这些通知? 这是我的代码...多个通知与android中的一个状态栏图标

public void onReceive(Context context, Intent intent) { 
     Toast.makeText(context, "coming", Toast.LENGTH_LONG).show(); 
     Bundle descBundle = intent.getExtras(); 
     CharSequence desc = descBundle.getString("description"); 
     int reminderId = descBundle.getInt("reminderId"); 
     NotificationManager mNotificationManager; 
     mNotificationManager = (NotificationManager) context 
       .getSystemService(Context.NOTIFICATION_SERVICE); 
     PendingIntent contentIntent = PendingIntent.getActivity(context, 
       reminderId, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT); 
     RemoteViews contentView = new RemoteViews(context.getPackageName(), 
       R.layout.main); 
     contentView.setImageViewResource(R.id.image, R.drawable.reminder_1); 
     contentView.setTextViewText(R.id.text, desc); 
     Notification notifyDetails = new Notification(); 
     notifyDetails.icon = R.drawable.reminder_1; 
     notifyDetails.when = System.currentTimeMillis(); 
     notifyDetails.tickerText = desc; 
     notifyDetails.iconLevel = 1; 
     notifyDetails.number = reminderId; 
     notifyDetails.contentView = contentView; 
     notifyDetails.contentIntent = contentIntent; 
     mNotificationManager.notify(0, notifyDetails); 
    } 

我使用此代码显示通知...但它仅显示一个通知内容...但是图标不显示的通知...

+0

我们可以在该管理器中添加多个通知吗? – Kandha 2010-09-07 09:11:20

回答

2

每个图标对应一个通知;您无法将多个通知与通知栏上的一个项目实例相关联。

但是,您可以像您的某个短信和电子邮件应用程序那样在您的图标上叠加一个数字(例如显示图标所代表的事件的数量)。

这是通过number实例变量Notification完成的,就像您在上面的代码片段中所做的那样。

编辑:
更清楚:如果你想多个通知,您需要创建多个Notification对象,并调用NotificationManager.notify()多次。

每个Notification只能生成一个图标,在通知区域内可以有一个内容,并且可以有一个与之关联的Intent

+0

我尝试了“数字”变量,但也显示单个通知。 – 100rabh 2010-12-18 06:40:00

+0

@ 100rabh:我编辑了我的答案,以便更清楚地说你不能这样做。 – 2010-12-18 09:13:40

3

请记住,如果您希望针对不同对象显示多个通知,则必须为每个通知分配一个不同的通知ID。

例如,如果你有2个不同的对象,你必须调用

mNotificationManager.notify(0, notifyDetails); 

mNotificationManager.notify(1, notifyDetails); 

如果你不这样做,通知将永远之一,将永远更新。