2014-09-03 93 views
0

我遇到了一个我正在创建的Android应用程序的问题。我有两个按钮和二者引起的通知按压之后,但在这里是问题进来,两者具有不变ID(1和2)如何在通知管理器(Android)上创建动态ID?

manager.notify(1, notification); 

这意味着notification1,总是出现在首先在状态栏中。当您先按通知2时,它没有任何区别,它始终是通知1,然后是通知2。现在我需要知道如何使动态ID

谁能告诉我一个解决方案,我可以如何避免不变的顺序?

这里的完整代码:

public void sendNotification(View view) { 

    switch(view.getId()){ 

     case R.id.button1: 
      Notification1(); 
      break; 

     case R.id.button2: 
      Notification2(); 
      break; 

     } 
    } 

private void Notification1() { 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
    builder.setAutoCancel(true); 
    builder.setContentTitle("BasicNotification"); 
    builder.setContentText("Test"); 
    builder.setSmallIcon(R.drawable.icon1); 

    Notification notification = builder.build(); 
    NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE); 
    manager.notify(1, notification); 

} 

private void Notification2() { 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
    builder.setAutoCancel(true); 
    builder.setContentTitle("BasicNotification"); 
    builder.setContentText("Test"); 
    builder.setSmallIcon(R.drawable.icon2); 

    Notification notification = builder.build(); 
    NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE); 
    manager.notify(2, notification); 

} 

回答

1

System.currentTimeInMillis()是生成唯一ID的我的首选方式。

manager.notify(System.currentTimeInMillis(), notification); 
+0

感谢您的快速anwser!但我必须去哪里但这呢?对不起,但我真的noobie :( – DudeEffects 2014-09-03 19:39:47

+0

.......见编辑 – r2DoesInc 2014-09-03 19:40:31

+0

感谢男人!!! omg你是最好的:D 我在德国论坛寻求帮助,但它需要几天的时间,然后它没有工作,现在我在一个英语论坛,我2min后得到一个简短而称职的awnser:D再次感谢你从几个小时的谷歌救济我^^ – DudeEffects 2014-09-03 19:50:34

0
//generating unique ID 
int uniqueID = (int) System.currentTimeMillis(); 

现在你可以使用在你的代码是这样

manager.notify(uniqueID , notification);