I'm creating a notification with RemoteViews
from a custom Service
, which is running with notification in a foreground mode (that is, service will remain active as long as notification is visible to user). Notification is set as Ongoing so user cannot swipe it off.
I'd like to change for example bitmap shown in ImageView
, contained within remote view's layout or change text value in a TextView
. Layout in remote view is set with XML layout file.
My problem is that once notification is created and visible to user, if I call any of RemoteViews
's functions like setImageViewResource()
to change Bitmap
shown in an ImageView
, the change is not visible, unless I do call setImageViewResource()
I call afterwards:
NotificationManager.notify( id, notification );
or
Service.startForeground(id,notification);
This doesn't sound right to me though. I can't believe that to update RemoteViews
UI in a notification that is already created, I have to re-initialize notification. If I have Button
control in a notification, it updates itself on touch and release. So there's gotta be a way to do this properly, but I don't know how.
Here is my code which creates notification inside my Service
instance:
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);
And function that 'forces' UI changes to notification:
public void updateNotiUI(){
this.startForeground(NOTIFICATION_ID, this.noti);
}
Within MyRemoteViews
class, when required, I do this to make changes to UI:
this.setImageViewResource(R.id.iconOFF, R.drawable.icon_off2);
this.ptMyService.updateNotiUI();
Can anyone tell me what is the correct way of updating UI components of a RemoteViews
in the notification?
See Question&Answers more detail:
os