首页 > 解决方案 > 在自定义内容通知中使用后更新 remoteView

问题描述

我正在尝试使用自定义 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);
    }

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

标签: javaandroidnotifications

解决方案


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

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

id您要更新的通知的唯一 ID在哪里,并且notification是使用新内容创建或更新的通知对象。

这个站点有一个如何在 Java 中更新通知的示例。


推荐阅读