gpt4 book ai didi

ForegroundService 通知的 android.os.TransactionTooLargeException

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:20:59 25 4
gpt4 key购买 nike

我正在编写一个 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

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

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

最佳答案

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

解决方案

我建议您仅在数据(距离、价格)发生变化时更新通知。

关于ForegroundService 通知的 android.os.TransactionTooLargeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51936819/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com