Я создаю уведомление с RemoteViews
из пользовательского Service
, который работает с уведомление в режиме переднего плана (то есть служба будет оставаться активной до тех пор, пока уведомление видно пользователю). Уведомление настроено как текущее, поэтому пользователь не может его отключить.
Я хотел бы изменить, например, растровое изображение, показанное в ImageView
, содержащееся в макете удаленного просмотра, или изменить текстовое значение в файле TextView
. Макет в удаленном просмотре задается файлом макета XML.
Моя проблема заключается в том, что после создания уведомления и его отображения пользователю, если я вызываю любую из функций RemoteViews
, например setImageViewResource()
для изменения Bitmap
, показанного в ImageView
, изменение не видно, пока я не вызову setImageViewResource()
. Я вызываю потом:
NotificationManager.notify( id, notification );
or
Service.startForeground(id,notification);
Хотя мне это кажется неправильным. Я не могу поверить, что для обновления RemoteViews
пользовательского интерфейса в уже созданном уведомлении мне нужно повторно инициализировать уведомление. Если у меня есть элемент управления 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);
И функция, которая «принуждает» пользовательский интерфейс к уведомлению:
public void updateNotiUI(){
this.startForeground(NOTIFICATION_ID, this.noti);
}
В классе MyRemoteViews
при необходимости я делаю это, чтобы внести изменения в пользовательский интерфейс:
this.setImageViewResource(R.id.iconOFF, R.drawable.icon_off2);
this.ptMyService.updateNotiUI();
Может ли кто-нибудь сказать мне, как правильно обновлять компоненты пользовательского интерфейса RemoteViews
в уведомлении?