RemoteServiceException:错误的startForeground通知:java.util.ConcurrentModificationException

时间:2019-02-16 02:28:29

标签: android android-service

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)

我已经收到此崩溃报告已有一段时间了。看来这仅发生在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() }
    }
}

每个频道都已设置,并且该应用程序可以在Android 8.0.0及更高版本的设备上运行,并且在测试过程中没有任何问题,除非我无法重现崩溃。

我想知道为什么会发生这种崩溃以及如何解决它。

谢谢。

3 个答案:

答案 0 :(得分:2)

我还随机在Android 8.0.0设备上遇到此崩溃。

研究AOSP源代码似乎是由于以下这一行引起的: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);
}

不确定该catch块上方try块中的确切行会导致它,但我假设AOSP本身存在错误。它与通知异步运行。 因此,在AOSP也在其上运行的同时,应用程序更新其通知的机会非常高。

答案 1 :(得分:0)

我的应用有时会更改前台通知。使用下面的代码后,有一段时间没有看到崩溃了。但我不确定,它可以解决所有情况。

pi = 22/7
while True:

    n = int(input('Please enter how many decimals you want to print: '))

    if n<=15:
        print('The output with {} decimal places is: '.format(n))
        x = str(pi)
        print(x[0:n+2])
        break
    else:
        print('Please enter a number between 0 and 15')

答案 2 :(得分:0)

华为?对我而言,仅在Firebase Crashlytics中报告了华为设备。因此这可能是该制造商的失败

相关问题