我正在使用RemoteViews
custom创建一个通知Service
,该通知在前台模式下与通知一起运行(也就是说,只要通知对用户可见,服务就会保持活动状态)。通知设置为进行中,因此用户无法将其关闭。
我想更改例如ImageView
包含在远程视图布局中的位图或更改TextView
. 远程视图中的布局使用 XML 布局文件设置。
我的问题是,一旦创建通知并且对用户可见,如果我调用任何RemoteViews
's 函数,例如setImageViewResource()
更改Bitmap
显示在 anImageView
中,更改是不可见的,除非我调用setImageViewResource()
我之后调用:
NotificationManager.notify( id, notification );
或者
Service.startForeground(id,notification);
不过,这对我来说并不正确。我不敢相信要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);
以及“强制” UI 更改为通知的功能:
public void updateNotiUI(){
this.startForeground(NOTIFICATION_ID, this.noti);
}
在MyRemoteViews
课堂上,当需要时,我这样做是为了对 UI 进行更改:
this.setImageViewResource(R.id.iconOFF, R.drawable.icon_off2);
this.ptMyService.updateNotiUI();
谁能告诉我RemoteViews
在通知中更新 a 的 UI 组件的正确方法是什么?