android现代化后台任务WorkManager,kotlin
Posted zhangphil
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android现代化后台任务WorkManager,kotlin相关的知识,希望对你有一定的参考价值。
android现代化后台任务WorkManager,kotlin(1)
添加引用:
def work_version = "2.8.0"
// (Java only)
implementation("androidx.work:work-runtime:$work_version")
// Kotlin + coroutines
implementation("androidx.work:work-runtime-ktx:$work_version")
// optional - GCMNetworkManager support
implementation("androidx.work:work-gcm:$work_version")
// optional - Multiprocess support
implementation "androidx.work:work-multiprocess:$work_version"
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.work.*
val TAG = "zhangphil"
class MyWorker(context: Context, wp: WorkerParameters) :
Worker(context, wp)
override fun doWork(): Result
repeat(10)
Thread.sleep(500)
Log.d(TAG, "work-" + it)
return Result.success()
class MainActivity : AppCompatActivity()
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
val request: WorkRequest =
OneTimeWorkRequestBuilder<MyWorker>()
.build()
WorkManager
.getInstance(this)
.enqueue(request)
以上是关于android现代化后台任务WorkManager,kotlin的主要内容,如果未能解决你的问题,请参考以下文章
Android Jetpack架构组件之WorkManager
用于在后台运行日常任务的 Android WorkManager api