1. WorkManager
1). 简介
其实就是"管理一些要在后台工作的任务, -- 即使你的应用没启动也能保证任务能被执行",WorkManager在底层, 会根据你的设备情况, 选用JobScheduler, Firebase的JobDispatcher, 或是AlarmManager。WorkManager并不是为了那种在应用内的后台线程而设计出来的. 这种需求你应该使用ThreadPool。
2). 引入
- kolin
# Android Studio 3.0, 3.0以下版本请使用complie
ext.work_version = "1.0.0-alpha02"
// WorkManager
implementation "android.arch.work:work-runtime:$work_version" // use -ktx for Kotlin
// optional - Firebase JobDispatcher support
implementation "android.arch.work:work-firebase:$work_version"
// optional - Test helpers
androidTestImplementation "android.arch.work:work-testing:$work_version"
- java
# 必须加入
implementation "android.arch.work:work-runtime:1.0.0-alpha01"
2. 用法
1). 执行一次的任务
- PullWorker.kt
/**
* Pull 任务
* 创建步骤:
* 1. 继承Worker类
* 2. 重写doWork()方法
* Created by mazaiting on 2018/6/6.
*/
class PullWorker : Worker() {
override fun doWork(): WorkerResult {
// 模拟设置页面中的"是否接受推送"是否被勾选
val isOkay = this.inputData.getBoolean("key_accept_bg_work", false)
// 判断是否推送
if (isOkay) {
// 模拟长时间工作
Thread.sleep(5000)
// 获取数据
val pulledResult = startPull()
// 设置输出数据
val output = Data.Builder().putString("key_pulled_result", pulledResult).build()
// 存放数据
outputData = output
// 返回成功
return WorkerResult.SUCCESS
} else {
// 返回失败
return WorkerResult.FAILURE
}
}
/**
* 开始推送
*/
private fun startPull(): String {
return "[worker] pull messages from backend."
}
}
- PullEngine.kt
/**
* Pull 引擎
* Created by mazaiting on 2018/6/6.
*/
class PullEngine {
fun scheduleOneTimeWork(context: Context) {
// 设置任务,执行一次
val pullRequest = OneTimeWorkRequestBuilder<PullWorker>()
.setInputData(
Data.Builder()
.putBoolean("key_accept_bg_work", true)
.build()
)
.build()
WorkManager.getInstance().enqueue(pullRequest)
// 将请求入列
WorkManager.getInstance().enqueue(pullRequest)
// 获取请求ID
val pullRequestId = pullRequest.id
// 存在SharedPreference中
PreferenceManager
.getDefaultSharedPreferences(context)
.edit()
.putString("pullId", pullRequestId.toString())
.apply()
}
}
- MainApplication.kt(注意在AndroidManifest.xml文件中配置)
class MainApplication : Application() {
override fun onCreate() {
super.onCreate()
// 任务执行一次
PullEngine().scheduleOneTimeWork(this.applicationContext)
}
}
- MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// UUID实现了Serializable接口,能由toString(),fromString()与String互转
val uuid = UUID.fromString(
PreferenceManager.getDefaultSharedPreferences(this.applicationContext)
.getString("pullId", "")
)
WorkManager.getInstance()
// 获取状态ID
.getStatusById(uuid)
// 观察数据
.observe(this, Observer<WorkStatus>{ status ->
if (null != status && status.state.isFinished) {
val pulledResult = status.outputData.getString("key_pulled_result","")
Log.e("MainActivity", pulledResult)
}
})
}
}
-
打印结果
2). 任务设置约束
fun scheduleOneTimeWork(context: Context) {
val constraints = Constraints.Builder()
// 负荷
.setRequiresCharging(true)
.build()
// 设置任务,执行一次
val pullRequest = OneTimeWorkRequestBuilder<PullWorker>()
// 设置约束
.setConstraints(constraints)
// 输入数据
.setInputData(
Data.Builder()
.putBoolean("key_accept_bg_work", true)
.build()
)
.build()
// 将请求入列
WorkManager.getInstance().enqueue(pullRequest)
// 获取请求ID
val pullRequestId = pullRequest.id
// 存在SharedPreference中
PreferenceManager
.getDefaultSharedPreferences(context)
.edit()
.putString("pullId", pullRequestId.toString())
.apply()
}
3). 取消任务
// 获取请求ID
val pullRequestId = pullRequest.id
// 取消任务
WorkManager.getInstance().cancelWorkById(pullRequestId)
4). 任务链
WorkManager.getInstance()
.beginWith(workA)
// Note: WorkManager.beginWith() returns a
// WorkContinuation object; the following calls are
// to WorkContinuation methods
.then(workB) // FYI, then() returns a new WorkContinuation instance
.then(workC)
.enqueue()
或
WorkManager.getInstance()
// First, run all the A tasks (in parallel):
.beginWith(workA1, workA2, workA3)
// ...when all A tasks are finished, run the single B task:
.then(workB)
// ...then run the C tasks (in any order):
.then(workC1, workC2)
.enqueue()
再或
val chain1 = WorkManager.getInstance()
.beginWith(workA)
.then(workB)
val chain2 = WorkManager.getInstance()
.beginWith(workC)
.then(workD)
val chain3 = WorkContinuation
.combine(chain1, chain2)
.then(workE)
chain3.enqueue()
5). 定时任务
/**
* 执行任务
*/
fun schedulePeriodicWork(context: Context) {
val constraints = Constraints.Builder()
// 负荷
.setRequiresCharging(true)
.build()
// 设置任务执行时长,并设置输入参数
// 长期任务,循环
val pullRequest = PeriodicWorkRequestBuilder<PullWorker>(30, TimeUnit.SECONDS)
.setConstraints(constraints)
.setInputData(
Data.Builder()
.putBoolean("key_accept_bg_work", true)
.build()
)
.build()
// 将请求入列
WorkManager.getInstance().enqueue(pullRequest)
// 获取请求ID
val pullRequestId = pullRequest.id
// 存在SharedPreference中
PreferenceManager
.getDefaultSharedPreferences(context)
.edit()
.putString("pullId", pullRequestId.toString())
.apply()
}