【发布时间】:2014-05-12 10:41:47
【问题描述】:
我正在使用自定义Service 中的RemoteViews 创建一个通知,该通知在前台模式下与通知一起运行(也就是说,只要通知对用户可见,服务就会保持活动状态)。通知设置为进行中,因此用户无法将其关闭。
我想更改例如ImageView 中显示的位图,包含在远程视图的布局中或更改TextView 中的文本值。远程视图中的布局使用 XML 布局文件设置。
我的问题是,一旦通知被创建并且对用户可见,如果我调用RemoteViews 的任何函数,如setImageViewResource() 来更改ImageView 中显示的Bitmap,则更改不可见,除非我确实打电话给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 的 UI 组件的正确方法是什么?
【问题讨论】:
标签: android android-notifications android-remoteview