美文网首页
WorkManage

WorkManage

作者: 有点健忘 | 来源:发表于2018-06-11 14:01 被阅读104次

    简单学习

    首先导入库

    implementation "android.arch.work:work-runtime-ktx:1.0.0-alpha01"
    

    然后简单操作,因为还没看别人咋写的,就知道有这个类,就看提示有啥方法开始操作
    简单写了3个worker,就是延迟在一个文件里写下当前时间而已。

        fun startWork() {
            var constraint = Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build()
            var inputData = Data.Builder().putString("title", "title1").build()
    
            var reques = OneTimeWorkRequest.Builder(MyWorker::class.java).setInitialDelay(20, TimeUnit.SECONDS).addTag("work1").setConstraints(constraint).build()
            var reques2 = OneTimeWorkRequest.Builder(MyWorker::class.java).setInitialDelay(50, TimeUnit.SECONDS).addTag("work2").setInputData(inputData).build()
            var reques3 = OneTimeWorkRequest.Builder(MyWorker::class.java).setInitialDelay(50, TimeUnit.SECONDS).addTag("work3").build()
            WorkManager.getInstance().beginWith(reques, reques2).then(reques3).enqueue()
        }
    
        fun getStatus() {
            WorkManager.getInstance().getStatusesByTag("work1")?.observeForever(object :android.arch.lifecycle.Observer<MutableList<WorkStatus>> {
                override fun onChanged(t: MutableList<WorkStatus>?) {
                    println("00=================${t?.size}")
                    (0 until (t?.size?:0)).forEach {
                        var workstaus = t!![it]
                        println("${it}==============${workstaus.id}====${workstaus.state}//${workstaus.state.isFinished}==${workstaus.tags}")
                    }
                }
            })
            println("111===========${WorkManager.getInstance().getStatusesByTag("work2")}====${WorkManager.getInstance().getStatusesByTag("work2")?.value?.size}")
        }
    
        public class MyWorker : Worker() {
            var formate = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
            override fun doWork(): WorkerResult {
                var smg = formate.format(Date(System.currentTimeMillis()))
                inputData.apply {
                    smg = this.getString("title", "") +"  "+ smg
                }
                UtilNormal.saveWords(smg) //就是把这个字符串写入sdcard的文件。
                return WorkerResult.SUCCESS
            }
        }
    
    补充

    Constraints.Builder().setRequiresCharging(false) 这个boolean值的作用,如果设置为true,那么必须插上电源这个任务才会运行。 这里插上电源,我试了下,必须是交流电。用数据线连接到电脑上的不算。
    begin(a).then(b)的效果,a执行完,并且dowork()返回WorkerResult.SUCCESS ,b才会执行。
    如果a返回了FAILED,那么b不会执行的,如果a返回retry,那么a就会等待时机再次尝试,直到返回成功,b才会执行。

    测试中发现。点开start以后,我就打开应用程序,点了强制停止,然后发现过了一会它又成了运行状态了【强制停止不是灰色的,又可以点击了】
    而且studio的日志里看到下边的东西

    22:44:17.247 : Failed execv(/system/bin/dex2oat --runtime-arg -classpath --runtime-arg  --debuggable --instruction-set=arm --instruction-set-features=smp,-div,-atomic_ldrd_strd --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --runtime-arg -Xms64m --runtime-arg -Xmx512m --instruction-set-variant=cortex-a53 --instruction-set-features=default --dex-file=/data/app/com.charliesong.demo0327-1/split_lib_dependencies_apk.apk --oat-file=/data/dalvik-cache/arm/data@app@com.charliesong.demo0327-1@split_lib_dependencies_apk.apk@classes.dex) because non-0 exit status
    
    22:44:18.887 D/PackageManagerHelper: androidx.work.impl.background.systemjob.SystemJobService enabled
    22:44:18.887  D/Schedulers: Created SystemJobScheduler and enabled SystemJobService
    22:44:18.897  D/PackageManagerHelper: androidx.work.impl.background.firebase.FirebaseJobService could not be disabled
        java.lang.IllegalArgumentException: Component class androidx.work.impl.background.firebase.FirebaseJobService does not exist in com.charliesong.demo0327
    
    22:44:18.897  D/PackageManagerHelper: androidx.work.impl.background.systemalarm.SystemAlarmService disabled
    22:44:18.907  I/InstantRun: starting instant run server: is main process
    22:44:18.907  D/ForceStopRunnable: Application was force-stopped, rescheduling.
    22:44:18.987 D/SystemJobService: onStartJob for c4d275cb-10af-4c08-bc9b-0985f196cbd9
    22:44:18.987  D/SystemJobScheduler: Scheduling work ID c4d275cb-10af-4c08-bc9b-0985f196cbd9 Job ID 40
    22:44:18.987  D/SystemJobService: Job is already being executed by SystemJobService: c4d275cb-10af-4c08-bc9b-0985f196cbd9
    22:44:18.997  D/SystemJobScheduler: Scheduling work ID c4d275cb-10af-4c08-bc9b-0985f196cbd9 Job ID 41
    22:44:18.997  D/SystemJobScheduler: Scheduling work ID ce1865ad-3d30-4e0d-8474-f6d64307a973 Job ID 42
    22:44:18.997 /SystemJobScheduler: Scheduling work ID ce1865ad-3d30-4e0d-8474-f6d64307a973 Job ID 43
    22:44:18.997  D/Processor: Processor: processing c4d275cb-10af-4c08-bc9b-0985f196cbd9
    22:44:19.017 D/WorkerWrapper: Worker result SUCCESS for c4d275cb-10af-4c08-bc9b-0985f196cbd9
    22:44:19.027  D/Processor: Processor c4d275cb-10af-4c08-bc9b-0985f196cbd9 executed; isSuccessful = true, reschedule = false
    22:44:19.027 D/SystemJobService: c4d275cb-10af-4c08-bc9b-0985f196cbd9 executed on JobScheduler
    22:44:19.027 /SystemJobScheduler: Scheduling work ID ce1865ad-3d30-4e0d-8474-f6d64307a973 Job ID 44
    22:44:19.037 /SystemJobScheduler: Scheduling work ID ce1865ad-3d30-4e0d-8474-f6d64307a973 Job ID 45
    22:44:39.017 /SystemJobService: onStartJob for c4d275cb-10af-4c08-bc9b-0985f196cbd9
    22:44:39.017 /SystemJobService: Job is already being executed by SystemJobService: c4d275cb-10af-4c08-bc9b-0985f196cbd9
    22:44:39.017 /Processor: Processor: processing c4d275cb-10af-4c08-bc9b-0985f196cbd9
    22:44:39.027 com.charliesong.demo0327 E/WorkerWrapper: Status for c4d275cb-10af-4c08-bc9b-0985f196cbd9 is SUCCEEDED; not doing any work
    22:44:39.027 /Processor: Processor c4d275cb-10af-4c08-bc9b-0985f196cbd9 executed; isSuccessful = false, reschedule = false
    22:44:39.027 /SystemJobService: c4d275cb-10af-4c08-bc9b-0985f196cbd9 executed on JobScheduler
    22:44:48.087 /SystemJobService: onStartJob for ce1865ad-3d30-4e0d-8474-f6d64307a973
    22:44:48.087 /Processor: Processor: processing ce1865ad-3d30-4e0d-8474-f6d64307a973
    22:44:48.087 /SystemJobService: Job is already being executed by SystemJobService: ce1865ad-3d30-4e0d-8474-f6d64307a973
    22:44:48.107 /WorkerWrapper: Worker result SUCCESS for ce1865ad-3d30-4e0d-8474-f6d64307a973
    22:44:48.117 /WorkerWrapper: Setting status to enqueued for 95339398-f24f-439f-aab0-3dfbbe787d45
    22:44:48.117 /Processor: Processor ce1865ad-3d30-4e0d-8474-f6d64307a973 executed; isSuccessful = true, reschedule = false
    22:44:48.117 /SystemJobService: ce1865ad-3d30-4e0d-8474-f6d64307a973 executed on JobScheduler
    22:44:48.127 /SystemJobScheduler: Scheduling work ID 95339398-f24f-439f-aab0-3dfbbe787d45 Job ID 46
    22:44:48.127 /SystemJobScheduler: Scheduling work ID 95339398-f24f-439f-aab0-3dfbbe787d45 Job ID 47
    22:45:09.017 /SystemJobService: onStartJob for ce1865ad-3d30-4e0d-8474-f6d64307a973
    22:45:09.017 /Processor: Processor: processing ce1865ad-3d30-4e0d-8474-f6d64307a973
    22:45:09.017 /SystemJobService: Job is already being executed by SystemJobService: ce1865ad-3d30-4e0d-8474-f6d64307a973
    22:45:09.027 com.charliesong.demo0327 E/WorkerWrapper: Status for ce1865ad-3d30-4e0d-8474-f6d64307a973 is SUCCEEDED; not doing any work
    22:45:09.027 /Processor: Processor ce1865ad-3d30-4e0d-8474-f6d64307a973 executed; isSuccessful = false, reschedule = false
    22:45:09.027 /SystemJobService: ce1865ad-3d30-4e0d-8474-f6d64307a973 executed on JobScheduler
    22:45:10.017 /SystemJobService: onStartJob for ce1865ad-3d30-4e0d-8474-f6d64307a973
    22:45:10.017 /Processor: Processor: processing ce1865ad-3d30-4e0d-8474-f6d64307a973
    22:45:10.017 /SystemJobService: Job is already being executed by SystemJobService: ce1865ad-3d30-4e0d-8474-f6d64307a973
    22:45:10.027 com.charliesong.demo0327 E/WorkerWrapper: Status for ce1865ad-3d30-4e0d-8474-f6d64307a973 is SUCCEEDED; not doing any work
    22:45:10.027 /Processor: Processor ce1865ad-3d30-4e0d-8474-f6d64307a973 executed; isSuccessful = false, reschedule = false
    22:45:10.027 /SystemJobService: ce1865ad-3d30-4e0d-8474-f6d64307a973 executed on JobScheduler
    22:45:58.907 /SystemJobService: onStartJob for 95339398-f24f-439f-aab0-3dfbbe787d45
    22:45:58.907 /Processor: Processor: processing 95339398-f24f-439f-aab0-3dfbbe787d45
    22:45:58.917 /SystemJobService: Job is already being executed by SystemJobService: 95339398-f24f-439f-aab0-3dfbbe787d45
    22:45:58.927 /WorkerWrapper: Worker result SUCCESS for 95339398-f24f-439f-aab0-3dfbbe787d45
    22:45:58.927 /Processor: Processor 95339398-f24f-439f-aab0-3dfbbe787d45 executed; isSuccessful = true, reschedule = false
    22:45:58.927 /SystemJobService: 95339398-f24f-439f-aab0-3dfbbe787d45 executed on JobScheduler
    
    

    另外当start执行了N次以后,可以看到get到的也是N次的信息,除非卸载重装,否则start了n次tag为xxx的work,到时候getbytag就能获取N次

    06-10 22:53:50.047 com.charliesong.demo0327 I/System.out: 0==============2140871e-f14a-498e-bc41-e6728b91a7ae====SUCCEEDED//true==[work1]
    06-10 22:53:50.047 com.charliesong.demo0327 I/System.out: 1==============85ed6feb-b5d2-4c5b-a3c6-c005ea60054e====SUCCEEDED//true==[work1]
    06-10 22:53:50.047 com.charliesong.demo0327 I/System.out: 2==============87b3b5d9-000d-4ef2-ba7e-2c005fc29da8====SUCCEEDED//true==[work1]
    06-10 22:53:50.047 com.charliesong.demo0327 I/System.out: 3==============c4d275cb-10af-4c08-bc9b-0985f196cbd9====SUCCEEDED//true==[work1]
    

    不过有点奇怪的是,我start里就有3个任务,可每次都有4条日志,前2条的work ID是一样的,就是job ID 不一样,不懂。

    06-10 22:59:49.737 xxx D/SystemJobScheduler: Scheduling work ID bbef65fb-aca0-4b8f-96a1-50da7234e5c8 Job ID 8
    06-10 22:59:49.737 xxx D/SystemJobScheduler: Scheduling work ID bbef65fb-aca0-4b8f-96a1-50da7234e5c8 Job ID 9
    06-10 22:59:49.737 xxxx D/SystemJobScheduler: Scheduling work ID cb3f7b83-9b8b-4e8d-aeb3-915d2498ff3f Job ID 10
    06-10 22:59:49.737 xxx D/SystemJobScheduler: Scheduling work ID cb3f7b83-9b8b-4e8d-aeb3-915d2498ff3f Job ID 11
    

    测试

    卸载重装app,然后开启一个任务

            val oneTimeRequest= OneTimeWorkRequestBuilder<Worker2>().setConstraints(constraints).setInputData(data)
                    .addTag("worker2")
                    .build()
    
            WorkManager.getInstance().beginWith(oneTimeRequest).enqueue()
    
         class Worker2:Worker() {
            override fun doWork(): WorkerResult {
                println("start ${javaClass.simpleName}==================")
                Thread.sleep(3000)
                println("end ${javaClass.simpleName}================")
                val date=SimpleDateFormat("yyyyMMDD_HHmmss", Locale.getDefault()).format(Date())
                UtilFile.saveStringToTempFile("$date ${javaClass.simpleName}  ${inputData.getString("title","")} \r\n")
                return  WorkerResult.SUCCESS
            }
        }
    

    日志

    21:18:45.793  D/SystemJobScheduler: Scheduling work ID 17d724d0-757e-44e3-8814-1b7a933a3f46 Job ID 0
    21:18:45.793  D/SystemJobScheduler: Scheduling work ID 17d724d0-757e-44e3-8814-1b7a933a3f46 Job ID 1
    21:18:45.793  D/GreedyScheduler: Starting tracking for 17d724d0-757e-44e3-8814-1b7a933a3f46
    D/ConstraintTracker: NetworkStateTracker: initial state = [ Connected=true Validated=true Metered=false NotRoaming=true ]
    21:18:45.803  D/NetworkStateTracker: Registering broadcast receiver
    21:18:45.803  D/WorkConstraintsTracker: Constraints met for 17d724d0-757e-44e3-8814-1b7a933a3f46
    21:18:45.803  D/GreedyScheduler: Constraints met: Scheduling work ID 17d724d0-757e-44e3-8814-1b7a933a3f46
    21:18:45.803  D/WorkConstraintsTracker: Constraints met for 17d724d0-757e-44e3-8814-1b7a933a3f46
    21:18:45.803  D/GreedyScheduler: Constraints met: Scheduling work ID 17d724d0-757e-44e3-8814-1b7a933a3f46
    21:18:45.813  D/Processor: Processor: processing 17d724d0-757e-44e3-8814-1b7a933a3f46
    21:18:45.813  D/Processor: Work 17d724d0-757e-44e3-8814-1b7a933a3f46 is already enqueued for processing
    I/System.out: start Worker2==================
    21:18:45.873  D/ViewRootImpl: MSG_RESIZED_REPORT: ci=Rect(0, 24 - 0, 0) vi=Rect(0, 0 - 0, 0) or=2
    21:18:45.883  D/NetworkStateTracker: Network broadcast received
    21:18:45.923  I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@caf4359 time:15813324
    21:18:46.803  D/SystemJobService: onStartJob for 17d724d0-757e-44e3-8814-1b7a933a3f46
    21:18:46.803  D/Processor: Work 17d724d0-757e-44e3-8814-1b7a933a3f46 is already enqueued for processing
    21:18:46.803  D/SystemJobService: Job is already being executed by SystemJobService: 17d724d0-757e-44e3-8814-1b7a933a3f46
    21:18:48.843  I/System.out: end Worker2================
    21:18:48.843  D/WorkerWrapper: Worker result SUCCESS for 17d724d0-757e-44e3-8814-1b7a933a3f46
    21:18:48.853  D/Processor: Processor 17d724d0-757e-44e3-8814-1b7a933a3f46 executed; isSuccessful = true, reschedule = false
    21:18:48.853  D/SystemJobService: 17d724d0-757e-44e3-8814-1b7a933a3f46 executed on JobScheduler
    

    间隔时间重复执行某个work

    看文章发现有个PeriodicWorkRequest可以执行重复的任务,试了下结果发现没重复啊,后来点击源码才看到,这玩意默认的最小间隔是15分钟。

        fun startRepeateWork(){
            var request=PeriodicWorkRequest.Builder(MyWorker::class.java,1,TimeUnit.MINUTES).addTag("hello").build()
            WorkManager.getInstance().enqueue(request)
            UtilNormal.saveUUID(this,request.id.toString())
        }
    
        fun getRepeatWorkStatus(){
            var str=UtilNormal.getUUID(this)
            if(TextUtils.isEmpty(str)){
                return
            }
            WorkManager.getInstance().getStatusById(UUID.fromString(str))?.apply {
                this.removeObserver(observerOnly)
                this.observeForever(observerOnly)
            }
        }
    
        val observerOnly=object:Observer<WorkStatus>{
            override fun onChanged(t: WorkStatus?) {
                t?.apply {
                    println("onChanged==============${t.id}====${t.state}//${t.state.isFinished}==${t.tags}")
                }
            }
        }
    

    相关文章

      网友评论

          本文标题:WorkManage

          本文链接:https://www.haomeiwen.com/subject/tsfheftx.html