首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将不同的Extras传递给不同的setDisplayIntent,但接收相同的Extras

将不同的Extras传递给不同的setDisplayIntent,但接收相同的Extras
EN

Stack Overflow用户
提问于 2015-05-09 06:47:40
回答 1查看 105关注 0票数 1

我所处的情况是,我的Watch应用程序已经更新了数据,并将在多个页面中显示这些数据。

根据这个答案:https://stackoverflow.com/a/30133449/327402,我为我的每个页面创建了一个displayIntent。

代码语言:javascript
复制
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);

直到这里,它看起来还可以,但是当我读到每个通知上发生的事情时,我意识到数据是相同的。

这是我简化的活动:

代码语言:javascript
复制
@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”“案文3”

,但在现实中,“文本3”显示在每一个通知!

我该怎么解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-09 07:41:51

不要使用0作为您的PendingIntent的请求代码,而是为您的请求的每个对象使用不同的唯一值。例如。

代码语言:javascript
复制
int counter = 0;
for (Route aRoute : myRoutes) {
    // other code
    PendingIntent displayendingIntent  
         = PendingIntent.getActivity(getApplicationContext(), counter++, 
                             viewIntent, PendingIntent.FLAG_UPDATE_CURRENT);
   // other code
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30136947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档