【问题标题】:Passing different Extras to different setDisplayIntent, but same is received将不同的 Extras 传递给不同的 setDisplayIntent,但收到相同的
【发布时间】:2015-05-09 06:47:40
【问题描述】:

我的 Watch 应用程序已更新数据并将在多个页面中显示这些数据。

根据这个答案: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” 《文字二》 “文字3”

,但实际上,“Text 3”显示在每个通知中!

我应该如何解决这个问题?

【问题讨论】:

    标签: android android-intent notifications wear-os


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢。我现在觉得问这个问题很愚蠢:-p
    • 你不应该。文档对此并不清楚
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-09
    • 2012-06-20
    • 1970-01-01
    • 2021-07-10
    • 2015-05-07
    • 1970-01-01
    • 2018-06-01
    相关资源
    最近更新 更多