[英]android.os.TransactionTooLargeException for ForegroundService notification
I'm writing an android app that tracks the user's location and shows the distance, time and price of the trip in a notification, all this tracked in a ForegroundService
. 我正在编写一个跟踪用户位置的Android应用程序,并在通知中显示行程的距离,时间和价格,所有这些都在ForegroundService
进行跟踪。 The service tracks the location, price and the time, and I'm updating the notification every second. 该服务跟踪位置,价格和时间,我每秒都在更新通知。 I get this TransactionTooLargeException
in production for the very first notification update, it only happens on Samsung devices running Android 8.0+. 我在生产中获得了第一次通知更新的TransactionTooLargeException
,它只发生在运行Android 8.0+的三星设备上。
This is what's happening: 这就是发生的事情:
I call the start
method from the service: 我从服务中调用start
方法:
public void start(MeasureService service) {
service.startForeground(NOTIFICATION_ID, getNotification(0, 0, 0));
}
I call the update
method from the service: 我从服务中调用update
方法:
public void update(int price, float meters, long time) {
mNotificationManager.notify(NOTIFICATION_ID, getNotification(price, meters, time));
}
Actually, these calls are called right after one another, the crash is coming from the update
method. 实际上,这些调用是相互调用的,崩溃来自update
方法。 Can this (calling them one after the other) be a problem? 这个(一个接一个地称呼它们)是一个问题吗?
this is the getNotification
: 这是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();
}
where the getSmallView
and getBigView
methods are like this: getSmallView
和getBigView
方法是这样的:
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;
}
and this is how I create the notificationBuilder
: 这就是我创建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());
}
and the 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);
}
This is the crash log I get: 这是我得到的崩溃日志:
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
I found a lot of similar issues online, but they are usually about passing big chunks of data in the intent, which I believe I'm not doing. 我在网上发现了很多类似的问题,但它们通常是关于在意图中传递大块数据,我相信我没有这样做。
Any idea what I might be doing wrong? 知道我可能做错了什么吗?
i think problem is in updating Notification every seconds. 我认为问题在于每秒更新通知。
Solution 解
i suggest you should update notification only when data likes(distance, price) changed. 我建议您只有在数据喜欢(距离,价格)发生变化时才应更新通知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.