RemoteServiceException: Bad notification for start

2020-05-27 04:24发布

问题:

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.

回答1:

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.



回答2:

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())
    }
}