ForegroundService通知的android.os.TransactionTooLargeException

问题描述 投票:5回答:1

我正在编写一个跟踪用户位置的Android应用程序,并在通知中显示行程的距离,时间和价格,所有这些都在ForegroundService中跟踪。该服务跟踪位置,价格和时间,我每秒都在更新通知。我在第一次通知更新时将这个TransactionTooLargeException投入生产,它只发生在运行Android 8.0+的三星设备上。

这就是发生的事情:

我从服务中调用start方法:

public void start(MeasureService service) {
    service.startForeground(NOTIFICATION_ID, getNotification(0, 0, 0));
}

我从服务中调用update方法:

public void update(int price, float meters, long time) {
    mNotificationManager.notify(NOTIFICATION_ID, getNotification(price, meters, time));
}

实际上,这些调用是一个接一个地调用,崩溃来自update方法。这个(一个接一个地称呼它们)是一个问题吗?

这是getNotification

private Notification getNotification(int price, float meters, long seconds) {
    if (mNotificationBuilder == null) {
        createNotificationBuilder();
    }
    return mNotificationBuilder
            .setCustomContentView(getSmallView(price))
            .setCustomBigContentView(getBigView(price, seconds, meters))
            .build();
}

getSmallViewgetBigView方法是这样的:

private RemoteViews getSmallView(int price) {
    String priceText = ...;
    mSmallView.setTextViewText(R.id.price, priceText);
    return mSmallView;
}

private RemoteViews getBigView(int price, long seconds, float meters) {
    String priceText = ...;
    String timeText = ...;
    String distanceText = ...;
    mBigView.setTextViewText(R.id.price, priceText);
    mBigView.setTextViewText(R.id.time, timeText);
    mBigView.setTextViewText(R.id.distance, distanceText);
    return mBigView;
}

这就是我创建notificationBuilder的方式:

private void createNotificationBuilder() {
        NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_LOW);
        ((NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
        mNotificationBuilder = new NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID);

    mNotificationBuilder = mNotificationBuilder
            .setSmallIcon(R.drawable.icon)
            .setOngoing(true)
            .setContentIntent(getOpenMeasurePageIntent());
}

getOpenMeasurePageIntent

private PendingIntent getOpenMeasurePageIntent() {
    Intent launchMeasureIntent = new Intent(mContext, MainActivity.class);
    launchMeasureIntent.setAction(...);
    launchMeasureIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    return PendingIntent.getActivity(mContext, 0, launchMeasureIntent, 0);
}

这是我得到的崩溃日志:

Caused by: java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 560988 bytes
16  at android.app.NotificationManager.notifyAsUser(NotificationManager.java:319)
17  at android.app.NotificationManager.notify(NotificationManager.java:284)
18  at android.app.NotificationManager.notify(NotificationManager.java:268)
19  at com.myapp.service.measure.MyNotification.void update(int,float,long) <- this is called from the timer

我在网上发现了很多类似的问题,但它们通常是关于在意图中传递大块数据,我相信我没有这样做。

知道我可能做错了什么吗?

android android-notifications foreground-service
1个回答
0
投票

我认为问题在于每秒更新通知。

我建议您只有在数据喜欢(距离,价格)发生变化时才应更新通知。

© www.soinside.com 2019 - 2024. All rights reserved.