1.添加依賴
implementation(libs.androidx.work.runtime.ktx)
toml
[versions]
workKtx = "2.9.0"
[libraries]
# WorkManager
androidx-work-runtime-ktx = { group = "androidx.work", name = "work-runtime-ktx", version.ref = "workKtx" }
2.新建后臺(tái)任務(wù)盼铁,每一個(gè)后臺(tái)任務(wù)都需要繼承 Worker:
/**
* 后臺(tái)任務(wù)
*/
class FirstWorker(context: Context, params: WorkerParameters) : Worker(context, params) {
override fun doWork(): Result {
Log.d("MainActivity", "doWork: 任務(wù)已執(zhí)行")
return Result.success()
}
}
3.定時(shí)任務(wù)
// 創(chuàng)建方式一
// val request = OneTimeWorkRequest.Builder(FirstWorker::class.java).build()
// 創(chuàng)建方式二
val request = OneTimeWorkRequestBuilder<FirstWorker>().build()
4.將后臺(tái)加入WorkManager悄晃,后臺(tái)任務(wù)便會(huì)執(zhí)行凤价,但不會(huì)立即執(zhí)行,而是系統(tǒng)找到合適的時(shí)機(jī)執(zhí)行
// 加入任務(wù)隊(duì)列
WorkManager.getInstance(this).enqueue(request)
5.延時(shí)執(zhí)行
// 單次任務(wù)
val request = OneTimeWorkRequestBuilder<FirstWorker>()
.setInitialDelay(1, TimeUnit.MINUTES)
.build()
6.添加 Tag
添加 Tag 的作用是方便取消任務(wù)挖炬。
// 單次任務(wù)
// val request = OneTimeWorkRequest.Builder(FirstWorker::class.java).build()
val request = OneTimeWorkRequestBuilder<FirstWorker>()
.addTag("one")
.build()
7.重新執(zhí)行失敗的任務(wù)
如果任務(wù)執(zhí)行失敗揽浙,還可以重新執(zhí)行。
首先需要將任務(wù)執(zhí)行結(jié)果返回為 retry
意敛。
/**
* 后臺(tái)任務(wù)
*/
class FirstWorker(context: Context, params: WorkerParameters) : Worker(context, params) {
override fun doWork(): Result {
Log.d("MainActivity", "doWork: 任務(wù)已執(zhí)行")
return Result.retry()
}
}
然后通過(guò) setBackoffCriteria
方法設(shè)置重新執(zhí)行時(shí)間馅巷。
// 單次任務(wù)
val request = OneTimeWorkRequestBuilder<FirstWorker>()
.setInitialDelay(1, TimeUnit.MINUTES)
.addTag("one")
.setBackoffCriteria(BackoffPolicy.LINEAR, 5, TimeUnit.MINUTES)
.build()
8.取消任務(wù)
// 取消后臺(tái)任務(wù)
// WorkManager.getInstance(this).cancelAllWork()
WorkManager.getInstance(this).cancelWorkById(request.id)
// WorkManager.getInstance(this).cancelAllWorkByTag("one")
9.監(jiān)聽(tīng)任務(wù)執(zhí)行結(jié)果
// 監(jiān)聽(tīng)任務(wù)執(zhí)行結(jié)果
WorkManager.getInstance(this).getWorkInfoByIdLiveData(request.id).observe(this) {
if (it.state == WorkInfo.State.SUCCEEDED) {
Log.d("MainActivity", "任務(wù)執(zhí)行成功")
} else if (it.state == WorkInfo.State.FAILED) {
Log.d("MainActivity", "任務(wù)執(zhí)行失敗")
}
}
10.周期任務(wù)
// 周期性任務(wù),執(zhí)行周期不得小于15分鐘
// val request = PeriodicWorkRequest.Builder(FirstWorker::class.java, 15, TimeUnit.MINUTES).build()
val request = PeriodicWorkRequestBuilder<FirstWorker>(15,TimeUnit.MINUTES).build()
11.鏈?zhǔn)饺蝿?wù)
beginWith
是第一個(gè)執(zhí)行的任務(wù)
then
是依次排在第一個(gè)任務(wù)后面的任務(wù)草姻。
只有當(dāng)任務(wù)執(zhí)行成功時(shí)钓猬,才會(huì)執(zhí)行下一個(gè)任務(wù)。
// 鏈?zhǔn)饺蝿?wù)
/*WorkManager.getInstance(this)
.beginWith(syncRequest)
.then(compressRequest)
.then(uploadRequest)
.enqueue()*/
學(xué)習(xí)完整筆記:
// 單次任務(wù)
// val request = OneTimeWorkRequest.Builder(FirstWorker::class.java).build()
val request = OneTimeWorkRequestBuilder<FirstWorker>()
.setInitialDelay(1, TimeUnit.MINUTES)
.addTag("one")
.setBackoffCriteria(BackoffPolicy.LINEAR, 5, TimeUnit.MINUTES)
.build()
// 周期性任務(wù)撩独,執(zhí)行周期不得小于15分鐘
// val request = PeriodicWorkRequest.Builder(FirstWorker::class.java, 15, TimeUnit.MINUTES).build()
// val request = PeriodicWorkRequestBuilder<FirstWorker>(15,TimeUnit.MINUTES).build()
// 加入任務(wù)隊(duì)列
WorkManager.getInstance(this).enqueue(request)
// 取消后臺(tái)任務(wù)
// WorkManager.getInstance(this).cancelAllWork()
// WorkManager.getInstance(this).cancelWorkById(request.id)
// WorkManager.getInstance(this).cancelAllWorkByTag("one")
// 監(jiān)聽(tīng)任務(wù)執(zhí)行結(jié)果
WorkManager.getInstance(this).getWorkInfoByIdLiveData(request.id).observe(this) {
if (it.state == WorkInfo.State.SUCCEEDED) {
Log.d("MainActivity", "任務(wù)執(zhí)行成功")
} else if (it.state == WorkInfo.State.FAILED) {
Log.d("MainActivity", "任務(wù)執(zhí)行失敗")
}
}
// 鏈?zhǔn)饺蝿?wù)
/*WorkManager.getInstance(this)
.beginWith(syncRequest)
.then(compressRequest)
.then(uploadRequest)
.enqueue()*/