notification复用中抛出can not parcel recyle’s bitmap

来源:互联网 发布:定时短信软件 编辑:程序博客网 时间:2024/04/26 00:28

由于对android重用思想的根深蒂固,所以我在使用通知的使用并不是stop之后再次start,而是只存在一个对象,直接startforgeound,

但是在remoteview中将之前bitmap.recyle时,会出现can not parcel recyle’s bitmap,bitmap

原始代码是这样的.

bitmap(之前通知现实的图片)

bitmap.recyle();

mRemoteView.setImageViewResource(R.id.notification_bg, R.drawable.log);

startForeground();

 

foreground死掉了,为什么呢?我发现每次执行与bitmap相关的方法,remoteviews中的bitmapcache都会去获取ID,这个ID就是bitmapcache中的index,因为removateviews的缺陷,导致并非我们设置bitmapnull时便会移除原本的action(remoteviews中的内部类,每次执行关于bitmap的方法都会new一个新的,添加到默认数组中),这就导致bitmapcache中的arraylist<bitmap>只增不减,为此我们无法将bitmap进行recyle,如果我们recyle便会抛出can not parcel recyle’s bitmap 错误,因为它竟然将bitmapcache中的arraylist<bitmap>全部parcel,如果我们不recyle,就会导致gc亚历山大的问题(我是想重用notification的,虽然google不建议我们重用,因为可能引起ANR,但是呢,重用如果只有单一功能的notification,重用还是有价值的,毕竟用户不希望在重复刷新nofication时,内存直升太过明显,那显然是很操蛋的体验,不要太相信GC线程,比如service只用了2M,而由于频繁操作,heap到达了10M,那么这个10M显示在应用程序列表里会持续很长时间),解决方法很简单:

private boolean removeBitmapCache() {try {Object obj = Tool.getField(mRemoteView, "mBitmapCache").get(mRemoteView);if (obj != null) {List<Bitmap> mps = (List<Bitmap>) Tool.getField(obj, "mBitmaps").get(obj);if (mps != null) {mps.clear();return true;}}} catch (Exception e) {e.printStackTrace();}return false;}


 

使用java反射,得到bitmapcachearraylist,进行清空.

0 0