相关疑难解决方法(0)

如何使用RemoteViews更新通知?

我正在使用RemoteViews自定义创建通知,该自定义Service以前台模式运行通知(即,只要用户可以看到通知,服务就会保持活动状态).通知设置为正在进行,因此用户无法将其滑动.

我想更改示例中显示的位图ImageView,包含在远程视图的布局中或更改文本值TextView.远程视图中的布局使用XML布局文件设置.

我的问题是,一旦通知被创建并且对用户可见,如果我调用任何RemoteViews类似于setImageViewResource()更改中Bitmap显示的任何功能ImageView,则更改是不可见的,除非我setImageViewResource()之后打电话给我打电话:

NotificationManager.notify( id, notification );
Run Code Online (Sandbox Code Playgroud)

要么

Service.startForeground(id,notification);
Run Code Online (Sandbox Code Playgroud)

这对我来说听起来不对.我无法相信要RemoteViews在已创建的通知中更新UI,我必须重新初始化通知.如果我对Button通知有控制权,它会在触摸和释放时自行更新.所以必须有一种方法可以做到这一点,但我不知道如何.

这是我的代码,它在我的Service实例中创建通知:

this.notiRemoteViews = new MyRemoteViews(this,this.getApplicationContext().getPackageName(),R.layout.activity_noti1);

Notification.Builder notibuilder = new Notification.Builder(this.getApplicationContext());
notibuilder.setContentTitle("Test");
notibuilder.setContentText("test");
notibuilder.setSmallIcon(R.drawable.icon2);
notibuilder.setOngoing(true);

this.manager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
this.noti = notibuilder.build();
this.noti.contentView = this.notiRemoteViews;
this.noti.bigContentView = this.notiRemoteViews;
this.startForeground(NOTIFICATION_ID, this.noti);
Run Code Online (Sandbox Code Playgroud)

并且"强制"UI更改为通知的功能:

public void updateNotiUI(){
    this.startForeground(NOTIFICATION_ID, this.noti);
}
Run Code Online (Sandbox Code Playgroud)

MyRemoteViews课堂上,如果需要,我这样做是为了对UI进行更改:

this.setImageViewResource(R.id.iconOFF, R.drawable.icon_off2);
this.ptMyService.updateNotiUI();
Run Code Online (Sandbox Code Playgroud)

谁能告诉我更新RemoteViews通知中UI组件的正确方法是什么?

android android-notifications android-remoteview

32
推荐指数
2
解决办法
2万
查看次数