【问题标题】:Updating the remoteView after being used in a custom content notification在自定义内容通知中使用后更新 remoteView
【发布时间】:2020-03-05 16:46:01
【问题描述】:

我正在尝试使用自定义 remoteView 创建媒体播放器控件通知。我遇到的问题是,每次我在远程视图中更改某些内容时,我都需要取消通知并从头开始重建它。这种方法对性能非常不利,因为它明显滞后。 (即将暂停图标更改为仅播放图标,我需要重建整个通知并重新加载专辑图像)

我尝试更改 remoteView,然后使用 NotificationBuilder.setCustomContentView 重新设置它,但它不起作用,这里是更改播放和暂停图标的代码:

public void PlayPause(){
        this.play=!this.play;
        if (this.play) {
            Bitmap toggleBmp = BitmapFactory.decodeResource(parent.getResources(), R.drawable.baseline_pause_black_48);
            remoteView.setImageViewBitmap(R.id.toggle, changeBitmapColor(toggleBmp,Color.parseColor(iconColor)));

        } else {
            Bitmap toggleBmp = BitmapFactory.decodeResource(parent.getResources(), R.drawable.baseline_play_arrow_black_48);
            remoteView.setImageViewBitmap(R.id.toggle, changeBitmapColor(toggleBmp,Color.parseColor(iconColor)));

        }

        nBuilder.setCustomContentView(remoteView);
    }

上面的代码不起作用,图标也没有改变。

【问题讨论】:

    标签: java android notifications


    【解决方案1】:

    您应该能够使用新信息更新通知,而不必取消原始通知。

    如果您使用唯一 ID 创建通知,则可以使用该 ID 通过 NotificationManager.notify(id, notification) 更新通知。

    其中id 是您要更新的通知的唯一 ID,notification 是使用新内容创建或更新的通知对象。

    site 提供了一个如何在 Java 中更新通知的示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-22
      • 2015-01-28
      • 2012-05-23
      • 2022-11-12
      相关资源
      最近更新 更多