由于51CTO博客维护中,且不好用,在简书平台更新。原博客地址:https://blog.51cto.com/4259297/1679401
近期做了一个音乐播放器,但是发现通知栏的RemoteView无法更新。
1.网络下载的图片无法更新

一般设置RemoteView就是通过上面的setContent方法,我之前是在调用setContent方法之前初始化化RemoteView。由于图片下载是需要一定时间的,所以nofication已经发出去了,remoteview再调用set方法也没有用。
所以我的做法是先把通知发出去,remoteView调用了set方法之后,一定记得再把notifycation发送一下。
private void updateRemoteView(Notification notification) {
if(isServiceDestroyed) return;
notification.contentView = remoteView;
notificationManager.notify(PID, notification); //调用了startForeground方法,就不需要这句代码了,否则会打开2个通知。
}
这个notify方法,只要pid一样,就只是起到更新notification的作用,所以不用担心多个显示多个通知栏的问题。
2.RemoteView set方法失效
在RemoteView随着Notification被notify出去之后,remoteView调用了set方法之后“失效”,其实并不是真的是set方法不起作用,而是上面说的得重新和Notification绑定一下,再notify。
3.PendingIntent
想通过点击通知栏跳转到另外一个界面,有2种实现方法:
第一种方式:通过builder

但是这种方式,对于6.0及以下系统,RemoteView的点击事件同时也会触发这个PendingIntent。
第二种方式:通过RemoteView的方法绑定点击事件和广播

在注册的广播里实现跳转

网友评论