[Android Kotlin 기초] 9-2. WorkerManager

WorkerManager

More information


The repository pattern


WorkerManager%20cd4a543530ce42db92a9eb92e436a5d5/Untitled.png

WorkerManager


The following classes in WorkManager library


  1. Worker
    • This class is where you define the actual work (the task) to run in the background. You extend this class and override the doWork() method. The doWork() method is where you put code to be performed in the background, such as syncing data with the server or processing images. You implement the Worker in this task
class RefreshDataWorker(appContext: Context,params:WorkerParameters)
    :CoroutineWorker(appContext,params) {

    companion object {
        const val WORK_NAME = "com.example.android.devbyteviewer.worker
														.RefreshDataWorker"
    }

    override suspend fun doWork(): Result {

        try {
            MarsApi.retrofitService.getProperties(
															OverviewViewModel.MarsApiFilter.SHOW_ALL.value)

					  Timber.d("Work request for sync is run")

        }catch (e:Exception){
            return Result.retry()
        }
        return Result.success()
    }
}
  1. WorkRequest
class DevByteApplication:Application() {

    private fun setupRecurringWork(){

			  val constraints =Constraints.Builder()
						            .setRequiredNetworkType(NetworkType.UNMETERED)
						            .setRequiresBatteryNotLow(true)
						            .setRequiresCharging(true)
						            .apply{
														if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.M){
								                    setRequiresDeviceIdle(true)
								                }
														}
												.build()

			 
       val repeatingRequest =
                PeriodicWorkRequestBuilder<RefreshDataWorker>(15,TimeUnit.MINUTES)
                        .setConstraints(constraints)
                        .build()
    }

}

WorkerManager%20cd4a543530ce42db92a9eb92e436a5d5/Untitled%201.png

  1. WorkManager
class DevByteApplication:Application() {

			override fun onCreate() {
        super.onCreate()
        delayedInit()
	    }

			private fun setupRecurringWork(){
					...
					...
					...
				 
			    WorkManager.getInstance().enqueueUniquePeriodicWork(
			            RefreshDataWorker.WORK_NAME,
			            ExistingPeriodicWorkPolicy.KEEP,
			            repeatingRequest)
				}

			private fun delayedInit(){
			        CoroutineScope(Dispatchers.Default).launch {
			            Timber.plant(Timber.DebugTree())
			            setupRecurringWork()
			        }
			    }
}

Homework


Question 1

What are the concrete implementations of the WorkRequest class? 2

▢ OneTimeWorkPeriodicRequest

▢ OneTimeWorkRequest and PeriodicWorkRequest

▢ OneTimeWorkRequest and RecurringWorkRequest

▢ OneTimeOffWorkRequest and RecurringWorkRequest

Question 2

Which of the following classes does the WorkManager use to schedule the background task on API 23 and higher? 1

▢ Only JobScheduler

▢ BroadcastReceiver and AlarmManager

▢ AlarmManager and JobScheduler

▢ Scheduler and BroadcastReceiver

Question 3

Which API do you use to add constraints to a WorkRequest? 1

▢ setConstraints()

▢ addConstraints()

▢ setConstraint()

▢ addConstraintsToWorkRequest()