Я создаю уведомление с RemoteViews
из пользовательской Службы, которая работает с уведомлением в режиме переднего плана (то есть служба будет оставаться активной, пока уведомление будет видимым для пользователя). Уведомление установлено как "Постоянно", поэтому пользователь не может его отключить.
Я хотел бы изменить, например, растровое изображение, показанное в ImageView
, содержащееся в макете удаленного представления или изменить текстовое значение в TextView
. Макет в удаленном представлении устанавливается с помощью файла макета xml
.
Моя проблема в том, что после того, как уведомление создано и видимо для пользователя, если я вызываю любую из RemoteViews
функций типа setImageViewResource
для изменения растрового изображения, показанного в ImageView
, изменение не отображается, если только после того, как я вызываю setImageViewResource
Я вызываю потом:
NotificationManager.notify( id, notification );
или
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
в уведомлении?