gpt4 book ai didi

android - 在 ContentProvider 中获取 WorkManager 实例

转载 作者:行者123 更新时间:2023-11-29 23:01:51 25 4
gpt4 key购买 nike

我想在内容提供者中安排任务:

class LogProvider : ContentProvider() {
override fun onCreate(): Boolean {
Log.i("Lagg", "Initializing LogCollection")
return try {
context?.let { LogCollectionInitializer().initializeCollector(it) }
true
} catch (e: Exception) {
false
}
}
// ...
}

初始化器安排工作的地方:

WorkManager.getInstance()
.enqueueUniquePeriodicWork(
CleanLogDatabaseTask.DB_CLEANER_TASK_NAME,
ExistingPeriodicWorkPolicy.KEEP,
PeriodicWorkRequestBuilder<CleanLogDatabaseTask>(DB_CLEANING_INTERVAL, TimeUnit.MILLISECONDS)
.setBackoffCriteria(BackoffPolicy.LINEAR, PeriodicWorkRequest.MIN_BACKOFF_MILLIS, TimeUnit.MILLISECONDS)
.build()
)

问题是我得到这个错误:

 java.lang.IllegalStateException: WorkManager is not initialized properly.  The most likely cause is that you disabled WorkManagerInitializer in your manifest but forgot to call WorkManager#initialize in your Application#onCreate or a ContentProvider.
  • 不,我没有禁用 WorkManager init。究竟是什么导致了这个问题?

当我从内容提供商访问 workManager 时,我应该采取不同的行动吗?

最佳答案

众所周知,WorkManager 有自己的内容提供程序。因此,如果您覆盖它并将其优先级设置得更高,它将在其他提供程序之前被初始化。所以在这里:

            android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="${applicationId}.workmanager-init"
android:initOrder="11"
android:enabled="true"
android:exported="false" /> ```

关于android - 在 ContentProvider 中获取 WorkManager 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56882863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com