Android Jetpack WorkManager 周期性任务/任务链与任务组合

Posted 安果移不动

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Jetpack WorkManager 周期性任务/任务链与任务组合相关的知识,希望对你有一定的参考价值。

周期性任务

      //周期性任务 //每隔15分钟去执行任务
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 
            PeriodicWorkRequest.Builder(MyWork::class.java, Duration.ofMinutes(15)).build()
        

任务链

第一个work

package com.anguomob.jecpack.work

import android.content.Context
import android.os.SystemClock
import android.util.Log
import androidx.work.Data
import androidx.work.Worker
import androidx.work.WorkerParameters

class MyWork(val context: Context, val workerParameters: WorkerParameters) :
    Worker(context, workerParameters) 
    private val TAG = "MyWork"
    override fun doWork(): Result 
        SystemClock.sleep(2000);
        val inputData = inputData.getString("input_data")

        Log.d(TAG, "MyWork doWork: $System.currentTimeMillis()")
        Log.d(TAG, "MyWork inputData: $inputData")
        //任务执行完毕 将数据返回
        val outputData = Data.Builder()
            .putString("output_data", "执行成功")
            .build()

        return Result.success(outputData)
    

第二个work

package com.anguomob.jecpack.work

import android.content.Context
import android.os.SystemClock
import android.util.Log
import androidx.work.Data
import androidx.work.Worker
import androidx.work.WorkerParameters

class MyWorkB(val context: Context, val workerParameters: WorkerParameters) :
    Worker(context, workerParameters) 
    private val TAG = "MyWorkB"
    override fun doWork(): Result 
        SystemClock.sleep(2000);
        val inputData = inputData.getString("input_data")

        Log.d(TAG, "MyWork doWork: $System.currentTimeMillis()")
        Log.d(TAG, "MyWork inputData: $inputData")
        //任务执行完毕 将数据返回
        val outputData = Data.Builder()
            .putString("output_data", "执行成功")
            .build()

        return Result.success(outputData)
    

代码

    fun addWork2(view: View) 
        //一种更简介的获取方式
        val work = OneTimeWorkRequest.from(MyWork::class.java)
        val workB = OneTimeWorkRequest.from(MyWorkB::class.java)

        //先执行 work  后执行workB
        WorkManager.getInstance(this)
            .beginWith(work)
            .then(workB)
            .enqueue()
    

 打印顺序将会是先第一个work 在workB 这样是链条样式的调用。所以叫任务链

现在有需求

 

a、b、c、d顺序不做要求。但是必须完成后才能执行e

WorkA代码

package com.anguomob.jecpack.work

import android.content.Context
import android.os.SystemClock
import android.util.Log
import androidx.work.Data
import androidx.work.Worker
import androidx.work.WorkerParameters

class MyWorkA(val context: Context, val workerParameters: WorkerParameters) :
    Worker(context, workerParameters) 
    private val TAG = javaClass.simpleName
    override fun doWork(): Result 
        SystemClock.sleep(2000);
        val inputData = inputData.getString("input_data")

        Log.d(TAG, "MyWork doWork: $System.currentTimeMillis()")
        Log.d(TAG, "MyWork inputData: $inputData")
        //任务执行完毕 将数据返回
        val outputData = Data.Builder()
            .putString("output_data", "执行成功")
            .build()

        return Result.success(outputData)
    

其他的B.C.D.E 都是改了类名而已不再贴出来

代码

    fun addWork2(view: View) 
        //一种更简介的获取方式
        val workA = OneTimeWorkRequest.from(MyWorkA::class.java)
        val workB = OneTimeWorkRequest.from(MyWorkB::class.java)
        val workC = OneTimeWorkRequest.from(MyWorkC::class.java)
        val workD = OneTimeWorkRequest.from(MyWorkD::class.java)
        val workE = OneTimeWorkRequest.from(MyWorkE::class.java)

        //先执行 workA,workB,workC,workE  后执行workE
        WorkManager.getInstance(this)
            .beginWith(arrayListOf(workA,workB,workC,workD))
            .then(workE)
            .enqueue()
    

日志

 

以上是关于Android Jetpack WorkManager 周期性任务/任务链与任务组合的主要内容,如果未能解决你的问题,请参考以下文章

Android Jetpack简介

Android Jetpack学习之旅--> 开始使用 Jetpack

Android Jetpack架构组件带你了解Android Jetpack

Android Jetpack简介

Android Jetpack架构组件——什么是Jetpack?

Android Jetpack架构组件——什么是Jetpack?