使用WorkManager时出现Java.util.concurrent.RejectedExecutionException

时间:2019-04-05 07:55:14

标签: android android-jetpack android-workmanager kotlin-coroutines

我正在使用工作经理和Kotlin协程

implementation "androidx.work:work-runtime-ktx:2.0.0"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.1.0'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.1.0'

我的工人阶级扩展了CoroutineWorker

我这样安排

private const val WORK_TAG = "myWork"
  private val constraints = Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build()

  fun schedule() {
         val work =
            OneTimeWorkRequestBuilder<MyWorker>().setConstraints(constraints).setInitialDelay(6, TimeUnit.HOURS)
                .addTag(WORK_TAG).build()
        WorkManager.getInstance().enqueue(work)
    }

在Google Play控制台中,我注意到某些Android版本(https://ibb.co/DzBkpzy)崩溃了

Fatal Exception: java.lang.RuntimeException: Error receiving broadcast Intent { act=android.net.conn.CONNECTIVITY_CHANGE flg=0x4000010 bqHint=4 (has extras) } in androidx.work.impl.constraints.trackers.NetworkStateTracker$NetworkStateBroadcastReceiver@2645398e
       at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:988)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:145)
       at android.app.ActivityThread.main(ActivityThread.java:7007)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

还有

Caused by java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@d6c9311 rejected from java.util.concurrent.ScheduledThreadPoolExecutor@1c2ffc76[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0]
       at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2011)
       at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:793)
       at java.util.concurrent.ScheduledThreadPoolExecutor.delayedExecute(ScheduledThreadPoolExecutor.java:298)
       at java.util.concurrent.ScheduledThreadPoolExecutor.schedule(ScheduledThreadPoolExecutor.java:503)
       at java.util.concurrent.Executors$DelegatedScheduledExecutorService.schedule(Executors.java:644)
       at androidx.work.impl.background.systemalarm.WorkTimer.startTimer(WorkTimer.java:82)
       at androidx.work.impl.background.systemalarm.DelayMetCommandHandler.onAllConstraintsMet(DelayMetCommandHandler.java:100)
       at androidx.work.impl.constraints.WorkConstraintsTracker.onConstraintMet(WorkConstraintsTracker.java:150)
       at androidx.work.impl.constraints.controllers.ConstraintController.updateCallback(ConstraintController.java:134)
       at androidx.work.impl.constraints.controllers.ConstraintController.onConstraintChanged(ConstraintController.java:141)
       at androidx.work.impl.constraints.trackers.ConstraintTracker.setState(ConstraintTracker.java:103)
       at androidx.work.impl.constraints.trackers.NetworkStateTracker$NetworkStateBroadcastReceiver.onReceive(NetworkStateTracker.java:170)
       at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:978)
       at android.os.Handler.handleCallback(Handler.java:739)
       at android.os.Handler.dispatchMessage(Handler.java:95)
       at android.os.Looper.loop(Looper.java:145)
       at android.app.ActivityThread.main(ActivityThread.java:7007)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

在崩溃报告日志中,我的代码没有看到任何一行。你能帮助我吗。谢谢

3 个答案:

答案 0 :(得分:3)

此问题已在更高版本的WorkManager中修复。您可以使用版本2.0.1-rc01或1.0.1-rc01。

答案 1 :(得分:0)

  

ScheduledThreadPoolExecutor @ 1c2ffc76 [已终止

工作经理试图将任务计划在终止的线程池上。由于该线程池不受您控制,并且这似乎不是一个普遍的问题,因此为此创建一个实际的修复程序将非常困难。

您最多可以尝试将其报告为androidx库中的错误,但是如果没有更多信息,就可能无法“修复”

答案 2 :(得分:0)

根据官方发布文档,已在2.2.0-rc01版中实现了修复。

  

修复了AlarmManager实施中的错误,该错误导致服务过早关闭并在极少数情况下导致RejectedExecutionException

https://developer.android.com/jetpack/androidx/releases/work#2.2.0-rc01