2014-09-03 94 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);