当我退出屏幕时,AndroidX worker 被取消
AndroidX worker is being canceled when i exit my screen
使用这个库“androidx.work:work-运行time-ktx:2.5.0”到运行后台线程。
但是 OneTimeWorkRequest 正在取消我退出屏幕的所有内容。我想要实现的是在应用程序级别设置它 运行 有人可以帮忙或一些提示吗?下面是我的代码:
var workManager = WorkManager.getInstance(requireContext())
val data: Data.Builder = Data.Builder().apply {
putLong("inspectionId", id)
}
val task = OneTimeWorkRequest.Builder(NewSyncWorkManager::class.java).apply {
// setConstraints(constraints)
setInputData(data.build())
setInitialDelay(1,TimeUnit.SECONDS)
addTag(id.toString())
}.build()
workManager.enqueue(task)
而 NewSyncWorkManager 如下:
class NewSyncWorkManager(context: Context,workerParams: WorkerParameters) :
CoroutineWorker(context, workerParams) {
override suspend fun doWork(): Result {
showNotification()
val foregroundInfo = ForegroundInfo(notificationId, notification!!)
setForeground(foregroundInfo)
if (inspectionId == 0L) {
Log.d("wah", "inspectionId to upload is missing, stopping")
return Result.failure()
}
val propertySections = sectionsRepo.getPropertyLayout(inspectionId)?.reportSections
.....
UPLOAD requests to the server
}
}
通过 运行 GlobalScope
中的 UPLOAD requests to the server
解决了问题
使用这个库“androidx.work:work-运行time-ktx:2.5.0”到运行后台线程。 但是 OneTimeWorkRequest 正在取消我退出屏幕的所有内容。我想要实现的是在应用程序级别设置它 运行 有人可以帮忙或一些提示吗?下面是我的代码:
var workManager = WorkManager.getInstance(requireContext())
val data: Data.Builder = Data.Builder().apply {
putLong("inspectionId", id)
}
val task = OneTimeWorkRequest.Builder(NewSyncWorkManager::class.java).apply {
// setConstraints(constraints)
setInputData(data.build())
setInitialDelay(1,TimeUnit.SECONDS)
addTag(id.toString())
}.build()
workManager.enqueue(task)
而 NewSyncWorkManager 如下:
class NewSyncWorkManager(context: Context,workerParams: WorkerParameters) :
CoroutineWorker(context, workerParams) {
override suspend fun doWork(): Result {
showNotification()
val foregroundInfo = ForegroundInfo(notificationId, notification!!)
setForeground(foregroundInfo)
if (inspectionId == 0L) {
Log.d("wah", "inspectionId to upload is missing, stopping")
return Result.failure()
}
val propertySections = sectionsRepo.getPropertyLayout(inspectionId)?.reportSections
.....
UPLOAD requests to the server
}
}
通过 运行 GlobalScope
中的UPLOAD requests to the server
解决了问题