簡體   English   中英

Foreground Coroutine Worker 導致 android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()

[英]Foreground Coroutine Worker causing android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()

在協程工作者上發生以下崩潰,該崩潰僅發生在某些三星、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 function 實現為,其中init() function 完成了我的工作。

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

我沒有使用任何位置或麥克風或任何默認前台服務類型。 這是一個做一些工作的普通工人。

在最新的 Workmanager API 中,您可以在 Worker 中使用“setExpedited”和“getForegroundInfo”而不是“setForeground”。 它會將您的工作標記為立即開始(滿足約束條件)。

腳步:

  • 覆蓋 getForegroundInfo 方法並返回 ForegroundInfo。
  • 在構建工作請求時,使用 setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) (有關更多配額政策,請查看下面的鏈接)
  • 並做了。

注意:setExpedited 僅適用於 OneTimeRequest 而不適用於 PeriodicWorkRequest

欲了解更多信息: setExpedited

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM