18
android.app.RemoteServiceException: Bad notification for startForeground: java.util.ConcurrentModificationException at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2204) at android.os.Handler.dispatchMessage(Handler.java:108) at android.os.Looper.loop(Looper.java:166) at android.app.ActivityThread.main(ActivityThread.java:7523) at java.lang.reflect.Method.invoke(Method.java:-2) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921) 

I've been receiving this crash report for quite some time. It seems that this only happens on Android 8.0.0.

@Synchronized override fun toForeground(id: Int) { fun action() { startForeground(id, builder?.build()) } if (Looper.myLooper() === Looper.getMainLooper()) { action() } else { Handler(Looper.getMainLooper()).post { action() } } } 

Every channel has set up and the app can be run on Android 8.0.0 and later devices without any problem during testing except I cannot reproduce the crash.

I'm wondering why this crash happens and how to fix it.

Thanks in advance.

8
  • Can you show stacktrace and/or code part? I suppose you use collection to handle bunch of notification messages - am I right? Commented Feb 16, 2019 at 2:42
  • @Raskilas I've added stacktrace and code. You're right and I handle bunch of notifications and make one of them foreground. But I still don't why it triggers ConcurrentModificationException. Commented Feb 16, 2019 at 5:27
  • Did you find any solution about this? Commented Jan 13, 2020 at 9:13
  • @yoonhok No. But I decide to ignore the crash for now because more and more users are using Android P or Q. Commented Jan 13, 2020 at 10:28
  • @Dewey Reed, that's too bad.. T.T... then do you know the reason? Or The reproduce scenario? Commented Jan 13, 2020 at 11:34

3 Answers 3

7

I also face this crash on Android 8.0.0 devices randomly.

Looking into AOSP source code it seems to result from this line: http://androidxref.com/8.0.0_r4/xref/frameworks/base/services/core/java/com/android/server/am/ServiceRecord.java#540:

public void postNotification() { ... } catch (RuntimeException e) { Slog.w(TAG, "Error showing notification for service", e); // If it gave us a garbage notification, it doesn't // get to be foreground. ams.setServiceForeground(name, ServiceRecord.this, 0, null, 0); ams.crashApplication(appUid, appPid, localPackageName, -1, "Bad notification for startForeground: " + e); } 

Not sure what line exactly in the try block above this catch block causes it, but I assume a bug in AOSP itself. It operates with the notification asynchronously. So the chance that the app updates its notification while AOSP also operates on it, is quite high.

Sign up to request clarification or add additional context in comments.

2 Comments

Did you find any solution about this?
for me it usually happens for users with Huawei devices (by reports of Firebase Crashlytics)
2

Huawei? For me it was reported only for Huawei devices in Firebase Crashlytics. So it could be a fail of this Manufacture. A really bad device https://dontkillmyapp.com/huawei

Comments

0

My app changes foreground notification sometimes. After I use the code below, I haven't seen the crash for some time. But I'm not sure that it can fix all scenarios.

private val toForegroundHandler: Handler = Handler(Looper.getMainLooper()) @Synchronized override fun toForeground(id: Int) { toForegroundHandler.removeCallbacksAndMessages(null) toForegroundHandler.postDelayed(16) { startForeground(id, builder.build()) } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.