android - Foreground Coroutine Worker 导致 android.app.RemoteServiceException : Context. startForegroundService() 然后没有调用 Service.startForeground()

标签 android kotlin-coroutines android-workmanager

在协程 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)
doWork worker 上的函数实现为,其中 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)
    }
}

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

最佳答案

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

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

  • 注意:setExpedited 仅适用于 OneTimeRequest 而不适用于 PeriodicWorkRequest
    欲了解更多信息:setExpedited

    关于android - Foreground Coroutine Worker 导致 android.app.RemoteServiceException : Context. startForegroundService() 然后没有调用 Service.startForeground(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65624725/

    相关文章:

    android - 刷新 Activity 时内存泄漏

    javascript - 错误代码1cordova插件文件传输android

    android - 当应用程序从任务管理器中终止时服务崩溃

    android - viewModelScope 未取消

    c# - 如何在 Xamarin Android 中实现 JobScheduler

    java - 通过IP获取设备MAC地址

    kotlin - 了解 Kotlin 的 yield 函数

    Kotlin协程如何取消async await()

    android - 无法使用 Hilt 注入(inject) workmanager 构造函数

    android - 如何从 Worker 类获取 ViewModel?