Skip to content

Commit c82e2d6

Browse files
author
Jill Heske
committed
Step.08-Solution-Schedule-Background-Work
1 parent 8b6182a commit c82e2d6

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

app/src/main/java/com/example/android/devbyteviewer/DevByteApplication.kt

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,49 @@
1818
package com.example.android.devbyteviewer
1919

2020
import android.app.Application
21+
import android.os.Build
22+
import androidx.work.*
23+
import com.example.android.devbyteviewer.work.RefreshDataWorker
24+
import kotlinx.coroutines.CoroutineScope
25+
import kotlinx.coroutines.Dispatchers
26+
import kotlinx.coroutines.launch
2127
import timber.log.Timber
28+
import java.util.concurrent.TimeUnit
2229

2330
/**
2431
* Override application to setup background work via WorkManager
2532
*/
2633
class DevByteApplication : Application() {
2734

28-
// TODO (01) Create CoroutineScope variable applicationScope, using Dispatchers.Default.
35+
val applicationScope = CoroutineScope(Dispatchers.Default)
2936

30-
// TODO (02) Create a delayedInit() function that calls setupRecurringWork() in
31-
// the coroutine you defined above.
32-
33-
// TODO (04) Create a setupRecurringWork() function and use a Builder to define a
34-
// repeatingRequest variable to handle scheduling work.
37+
private fun delayedInit() {
38+
applicationScope.launch {
39+
setupRecurringWork()
40+
}
41+
}
3542

36-
// TODO (05) In setupRecurringWork(), get an instance of WorkManager and
37-
// launch call enqueuPeriodicWork() to schedule the work.
43+
private fun setupRecurringWork() {
44+
val constraints = Constraints.Builder()
45+
.setRequiredNetworkType(NetworkType.UNMETERED)
46+
.setRequiresBatteryNotLow(true)
47+
.setRequiresCharging(true)
48+
.apply {
49+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
50+
setRequiresDeviceIdle(true)
51+
}
52+
}.build()
3853

39-
// TODO (07) In setupRecurringWork(), define constraints to prevent work from occurring when
40-
// there is no network access or the device is low on battery.
54+
val repeatingRequest
55+
= PeriodicWorkRequestBuilder<RefreshDataWorker>(1, TimeUnit.DAYS)
56+
.setConstraints(constraints)
57+
.build()
4158

42-
// TODO (08) Add the constraints to the repeatingRequest definition.
59+
WorkManager.getInstance().enqueueUniquePeriodicWork(
60+
RefreshDataWorker.WORK_NAME,
61+
ExistingPeriodicWorkPolicy.KEEP,
62+
repeatingRequest)
63+
}
4364

4465
/**
4566
* onCreate is called before the first screen is shown to the user.
@@ -50,6 +71,6 @@ class DevByteApplication : Application() {
5071
override fun onCreate() {
5172
super.onCreate()
5273
Timber.plant(Timber.DebugTree())
53-
// TODO (03) Call delayedInit().
74+
delayedInit()
5475
}
5576
}

app/src/main/java/com/example/android/devbyteviewer/work/RefreshDataWork.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ import retrofit2.HttpException
2727
class RefreshDataWorker(appContext: Context, params: WorkerParameters):
2828
CoroutineWorker(appContext, params) {
2929

30-
// TODO (06) Create a companion object and define a WORK_NAME constant.
30+
companion object {
31+
const val WORK_NAME = "RefreshDataWorker"
32+
}
3133

3234
/**
3335
* A coroutine-friendly method to do your work.

0 commit comments

Comments
 (0)