WorkerManager

根据google官方demo,自己仿写

google例子地址https://github.com/android/architecture-components-samples/tree/main/WorkManagerSample

MainActivity,调用位置

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        findViewById

BaseWorker类,用来做多个任务的组合,类似责任链模式

abstract class BaseWorker(context: Context, parameters: WorkerParameters) :
    CoroutineWorker(context, parameters) {
    override suspend fun doWork(): Result {
        val resourceUri = inputData.getString(Constants.KEY_IMAGE_URI)//获取传递过来的数据
        try {
            applyFilter(resourceUri!!)
            return Result.success()
        } catch (throwable: Throwable) {
            return Result.failure()
        }
    }

    abstract fun applyFilter(input: String): String
}

FirstWroker类,第一个任务类,继承BaseWorker,实现applyFilter方法

class FirstWroker(appContext: Context, workerParams: WorkerParameters) :
    BaseWorker(appContext, workerParams) {
    override fun applyFilter(input: String): String {
        var s = input+1;
        Log.d("testStr==1",s)
        return s
    }
}

TwoWorker类,第二个任务类,同样继承BaseWorker,实现applyFilter方法(和第一个没什么区别)

class TwoWorker(context: Context, parameters: WorkerParameters) : BaseWorker(context, parameters) {
    override fun applyFilter(input: String): String {
        var s = input + 2
        Log.d("testStr==2", s)
        return s
    }
}

TestOperations类,管理工作流程,比较重要,如果是多任务模式

class TestOperations constructor(val continuation: WorkContinuation) {
    class Builder(val mContext: Context, val testStr: String) {
        private var startFirst1: Boolean = false
        private var startTow2: Boolean = false
        fun setFirstWorker(startFirst: Boolean): Builder {
            startFirst1 = startFirst//
            return this
        }

        fun setTwoWorker(startTow: Boolean): Builder {
            startTow2 = startTow
            return this
        }

        fun build(): TestOperations {
            var continuation = WorkManager.getInstance(mContext)
                .beginUniqueWork(
                    Constants.TEST_KEY,
                    ExistingWorkPolicy.REPLACE,
                    OneTimeWorkRequest.from(CancelWorker::class.java)
                )//创建并添加多任务,beginUniqueWork可以放入多个不重复的任务,beginWith可以放入重复的
            if (startFirst1) {
                val test = OneTimeWorkRequestBuilder()
                    .setInputData(createInputData())
                    .build()
                continuation = continuation.then(test)//添加第一个任务,由于每个都继承BaseWorker,所以都会走Base的doWork方法
            }
            if (startTow2) {
                val test2 = OneTimeWorkRequestBuilder()
                    .setInputData(createInputData())
                    .build()
                continuation = continuation.then(test2)//添加第二个任务,由于每个都继承BaseWorker,所以都会走Base的doWork方法
            }
            return TestOperations(continuation)//返回给MainActivity,使MainActivity可以控制什么时候使用enqueue入队开启
        }

        private fun createInputData(): Data {
            //键值对获取地址参数,传递给不同的工作线程,比如google demo传递的是图片地址,所以会依次对图片做不同处理,顺序执行
            //如果为了获取上一个处理完的结果数据,理论上是可以通过键值对存储,依次修改,具体没有测试过
            return workDataOf(Constants.KEY_IMAGE_URI to testStr)
        }
    }

}

Constants,通用KEY

object Constants {
    const val TEST_KEY:String = "test_key" ;

    const val KEY_IMAGE_URI = "KEY_IMAGE_URI"
}

CancelWorker,做一些准备工作

class CancelWorker(context: Context, workerParams: WorkerParameters) :
    Worker(context, workerParams) {
    override fun doWork(): Result {
        //做一些准备工作
        return Result.success()
    }

}

你可能感兴趣的:(WorkerManager)