【问题标题】:Foreground Coroutine Worker causing android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()Foreground Coroutine Worker 导致 android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
【发布时间】:2021-01-08 07:06:26
【问题描述】:

在 coroutine worker 上发生以下崩溃,该崩溃仅发生在部分三星、Vivo 和 Oppo 设备上,并且仅在 Android 10 上发生

Fatal Exception: android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground(): ServiceRecord{2dcabaa u0 `app_id_hidden`/androidx.work.impl.foreground.SystemForegroundService}
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2159)
       at android.os.Handler.dispatchMessage(Handler.java:107)
       at android.os.Looper.loop(Looper.java:230)
       at android.app.ActivityThread.main(ActivityThread.java:7752)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:526)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1034)

worker 上的 doWork 函数实现为 init() 函数完成我的工作。

override suspend fun doWork(): Result {
    setForeground(createForegroundInfo())
    return init()
}

private fun init() {
    //implementation hidden
}

private fun createForegroundInfo(): ForegroundInfo {
    val notificationId = 1
    return ForegroundInfo(notificationId, createNotification())
}

private fun createNotification(): Notification {
    val context = applicationContext

    val builder = NotificationCompat.Builder(context, ChannelType.NOTIFICATION_CHANNEL_GENERAL)
                .setContentTitle("Message")
                .setSmallIcon(R.drawable.ic_notif_small)
                .setOngoing(true)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        createNotificationChannel(
                    ChannelType.NOTIFICATION_CHANNEL_GENERAL,
                    ChannelType.NOTIFICATION_CHANNEL_GENERAL
        ).also {
            builder.setChannelId(it.id)
        }
    }
    return builder.build()
}

@TargetApi(Build.VERSION_CODES.O)
private fun createNotificationChannel(
         channelId: String,
         name: String
    ): NotificationChannel {
        return NotificationChannel(
                channelId, name, NotificationManager.IMPORTANCE_LOW
        ).also { channel ->
            notificationManager.createNotificationChannel(channel)
    }
}

我没有使用任何位置或麦克风或任何默认前台服务类型。这是一个做一些工作的普通工人。

【问题讨论】:

  • 嗨,@Akash Amin 你找到答案了吗?我也面临同样的问题。
  • Android 问题跟踪器中有一些报告,但它们都以 Won't Fix 的形式关闭,例如issuetracker.google.com/issues/190412030。他们总是说这是因为某些东西过多地阻塞了主线程。就我而言,我也有几乎完全在三星设备上的崩溃分析报告。如果我的应用程序代码有问题,那么为什么它只发生在三星设备上?此外,我每个用户只看到一次崩溃,这也很奇怪。是不是只有在更新应用时才会发生?

标签: android kotlin-coroutines android-workmanager


【解决方案1】:

在最新的 Workmanager API 中,您可以在 Worker 中使用“setExpedited”和“getForegroundInfo”而不是“setForeground”。它会将您的工作标记为立即开始(满足约束条件)。

步骤:

  • 重写 getForegroundInfo 方法并返回 ForegroundInfo。
  • 在构建工作请求时,使用 setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)(有关更多配额政策,请查看下面的链接)
  • 完成了。

注意:setExpedited 仅适用于 OneTimeRequest 而不适用于 PeriodicWorkRequest

欲了解更多信息:setExpedited

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-09
    • 2019-01-09
    • 2018-03-29
    • 2018-03-04
    • 1970-01-01
    • 2019-09-17
    • 1970-01-01
    相关资源
    最近更新 更多