【问题标题】:RemoteServiceException Context.startForegroundService() did not then call Service.startForeground()RemoteServiceException Context.startForegroundService() 然后没有调用 Service.startForeground()
【发布时间】:2018-03-04 15:59:32
【问题描述】:

我在 Android 8.0 和 Android 7.x 中都通过 Fabric.io 收到此错误报告。

由于不只是特定的类失败,我不知道如何处理它们。

如果您有任何想法,请帮助我。

【问题讨论】:

    标签: android


    【解决方案1】:

    在 Android O 中,我们有一个新的背景限制。当你尝试 startService() 时,你会得到 IllegalStateException,所以现在你应该使用 startForegroundService(),但是如果你用这个新方法启动服务,你会得到像你的截图一样的错误。为避免此异常,您有 5 秒的时间在 startForegroundService() 之后执行 startForeground(),以通知用户您正在后台工作。

    那么,Android O 中只有一种方式:

    context.startForegroundService(intent)
    

    在服务 onCreate() 中做这样的事情:

    startForeground(1, new Notification());
    

    更新 所以我认为,一些制造商在 Android N 上向后移植了这些新的背景限制,这就是为什么你可以在 Android N 中获得相同的例外。

    【讨论】:

    • 感谢@Andrew Browiski。但我收到了 Android 8.0 和 7.x
    • @Hoang Duc Tuan,在 Android N_MR1 中你实际上不能使用 startForegroundService。也许你可以发布一些代码?
    • 我的代码:if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { context.startForegroundService(intent); } else { context.startService(intent); }
    • 我在MainActivityonCreate 启动服务。并在ServiceonCreateonStartCommand 中调用startForeground。应用程序在调用onCreate 时应始终处于前台状态。但我仍然收到此错误。
    • new Notification() 为 Android 版本 27 抛出错误。现在您需要提供有效的通知
    猜你喜欢
    • 2019-01-09
    • 2018-03-29
    • 2017-11-09
    • 2019-12-07
    • 1970-01-01
    • 1970-01-01
    • 2018-05-16
    • 2019-09-17
    相关资源
    最近更新 更多