2015-05-09 18 views
1

我處於Watch應用程序已更新數據並將在多個頁面中顯示這些數據的情況。將不同的附加功能傳遞給不同的setDisplayIntent,但收到的卻是相同的

根據這個答案:https://stackoverflow.com/a/30133449/327402,我爲每個頁面創建了一個displayIntent。

List extras = new ArrayList(); 

//This is a loop to create every individual Notification 
for (Route aRoute : myRoutes) { 
    Intent viewIntent = new Intent(getApplicationContext(), MainActivity.class); 
    String toSend = aRoute.detail; 
    Log.d("CVE",toSend); 
    viewIntent.putExtra("KEY", toSend); 

    PendingIntent displayendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,viewIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    Notification aNotif = new NotificationCompat.Builder(getApplicationContext()) 
     .setLargeIcon(bitmap) 
     .extend(new NotificationCompat.WearableExtender() 
     .setDisplayIntent(displayendingIntent) 
     .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM)) 
      .setSmallIcon(R.mipmap.ic_launcher) 
     .build(); 

    extras.add(aNotif); 
} 

//This is "main" notification. 
NotificationCompat.Builder builder1 = new NotificationCompat.Builder(this) 
    .setContentTitle(title) 
    .setContentText(desc) 
    .setSmallIcon(R.mipmap.ic_launcher); 

Notification notification = builder1 
         .extend(new NotificationCompat.WearableExtender() 
.addPages(extras)) 
.build(); 

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this); 
notificationManager.notify(0, notification); 

直到這裏,它看起來不錯,但是當我讀到每個通知會發生什麼時,我意識到數據是相同的。

這是我的簡化的活性:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    mTextView = (TextView) findViewById(R.id.text_view); 
    mTextView.setText("Value: "+getIntent().getStringExtra("KEY")); 
} 

的問題是,該日誌(見代碼的第一部分部分的第6行)正確地顯示:

「文本1」 「文本2「 」Text 3「

,但實際上,在每個通知中都會顯示」文本3「!

我該如何解決這個問題?

回答

2

不要使用0作爲您的PendingIntent的請求代碼,而是針對您的請求的每個對象使用不同的唯一值。例如。

int counter = 0; 
for (Route aRoute : myRoutes) { 
    // other code 
    PendingIntent displayendingIntent 
     = PendingIntent.getActivity(getApplicationContext(), counter++, 
          viewIntent, PendingIntent.FLAG_UPDATE_CURRENT); 
    // other code 
} 
+0

非常感謝。我現在覺得這個問題很愚蠢:-p –

+0

你不應該。文件不清楚 – Blackbelt

相關問題