关于 Notification 的 TransactionTooLargeException的问题

最近做计步器,需要每隔五秒更新通知栏的ui,

考虑的性能问题,notification不是每次都new

然后再后台发现报了如下问题:

android.os.TransactionTooLargeException: data parcel size 521616 bytes
    at android.os.BinderProxy.transactNative(Native Method)
    at android.os.BinderProxy.transact(Binder.java:617)
    at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:857)
    at android.app.NotificationManager.notifyAsUser(NotificationManager.java:325)
    at android.app.NotificationManager.notify(NotificationManager.java:293)
    at android.app.NotificationManager.notify(NotificationManager.java:277)
    at com.second.stepcount.utils.NotificationUtils.sendNotification(NotificationUtils.java:100)
    at com.second.stepcount.service.NotificationManager.showNotification(NotificationManager.kt:50)
    at com.second.stepcount.service.StepsService.updateStep(StepsService.java:142)
    at com.second.stepcount.service.StepsService.onSensorChanged(StepsService.java:199)
    at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:714)
    at android.os.MessageQueue.nativePollOnce(Native Method)
    at android.os.MessageQueue.next(MessageQueue.java:356)
    at android.os.Looper.loop(Looper.java:138)
    at android.app.ActivityThread.main(ActivityThread.java:6623)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)

查看

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();
    }
    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解决!

你可能感兴趣的:(关于 Notification 的 TransactionTooLargeException的问题)