最近做一个需求,需要每隔五秒更新通知栏的ui,
考虑的性能问题,notification不是每次都new
然后再后台发现报了如下问题:
android.os.TransactionTooLargeException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:508)
at android.service.notification.INotificationListener$Stub$Proxy.onNotificationPosted(INotificationListener.java:167)
at com.android.server.notification.NotificationManagerService$NotificationListeners.notifyPosted(NotificationManagerService.java:3536)
at com.android.server.notification.NotificationManagerService$NotificationListeners.access$5800(NotificationManagerService.java:3342)
at com.android.server.notification.NotificationManagerService$NotificationListeners$2.run(NotificationManagerService.java:3450)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at com.android.server.SystemServer.run(SystemServer.java:417)
at com.android.server.SystemServer.main(SystemServer.java:295)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1119)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
查看
setOnClickPendingIntent 的源码,发现
最终,系统是这么做的
* * @param a The action to add */ private void addAction(Action a) { if (hasLandscapeAndPortraitLayouts()) { throw new RuntimeException("RemoteViews specifying separate landscape and portrait" + " layouts cannot be modified. Instead, fully configure the landscape and" + " portrait layouts individually before constructing the combined layout."); } if (mActions == null) { mActions = new ArrayList<Action>(); } mActions.add(a); // update the memory usage stats a.updateMemoryUsageEstimate(mMemoryUsageCounter); }
mActions 没有考虑到add的内容是否重复。。。
所以,如果重复setOnClickPendingIntent 的话,就会产生上面的bug,
解决方案
===================》notification 复用了一百次()之后,选择丢弃,重新new 一个,
while (flag) { index++; if (index%100==0){//当到第一百次的时候重新init一个,之前的丢弃 initNotification(); } else { addNotificaiton(); } }
bug解决!