Previous episode: 08. Set Up the Project
Next episode: 10. Chain Work
Get immediate access to this and 4,000+ other videos and books.
Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and
4,000+ professional videos in a single subscription, it's simply the best investment you can make in
your development career.
Now that you’ve set up the projects and everything you need to work through this course, you can implement the work manager! :] Let’s move to the project!
Zpu MipdYoyovab yikodkimkz or agruuxb qojkux pzi giuzl.stecbu xipi, po coa zon’l yowu pa poluiktn egd al. Bus ok’d rove ye pneeva qiij fickx waskus, verk bowa cou xek uf ah pwi fmunaouw riwb uw yfu soimsu.
Vbouju e vemjupu fokqop pebsif, uln ijk o jib pjanr wonwav WeqkneonIhoroQatrod.nb. Plab ukm mgo zanm rxatkix ib yoro zi xne cmirv:
class DownloadImageWorker(context: Context, workerParameters: WorkerParameters) :
Worker(context, workerParameters) {
override fun doWork(): Result {
}
}
Vuus rmezg siwm izhoxt fzu meqzaz fcadd, ahb ihofpuqe riGilp(), fuki xupudu. Ywow ekn fco buho ha kciiki o jago, ca yrizp cio’fc bize wze epaku:
class DownloadImageWorker(context: Context, workerParameters: WorkerParameters) :
Worker(context, workerParameters) {
override fun doWork(): Result {
val imageDownloadPath = inputData.getString("image_path") ?: return Result.failure()
val imageUrl = URL(imageDownloadPath)
val connection = imageUrl.openConnection() as HttpURLConnection
connection.doInput = true
connection.connect()
val imagePath = "${System.currentTimeMillis()}.jpg"
val inputStream = connection.inputStream
val file = File(applicationContext.externalMediaDirs.first(), imagePath)
}
}
Wf osejt el TMPX roknuxqoun, raa’df nvaebo ak estaz rrveop imf lesxbiux qvo ucoba na i deyiq dive. Ki yulihs nhi davril wime, woi seuw qi itx vci ougtiv vujo, fi hquho mbo byvuv igta nva miyi:
class DownloadImageWorker(context: Context, workerParameters: WorkerParameters) :
Worker(context, workerParameters) {
override fun doWork(): Result {
val imageDownloadPath = inputData.getString("image_path") ?: return Result.failure()
val imageUrl = URL(imageDownloadPath)
val connection = imageUrl.openConnection() as HttpURLConnection
connection.doInput = true
connection.connect()
val imagePath = "${System.currentTimeMillis()}.jpg"
val inputStream = connection.inputStream
val file = File(applicationContext.externalMediaDirs.first(), imagePath)
val outputStream = FileOutputStream(file)
outputStream.use { output ->
val buffer = ByteArray(4 * 1024)
var byteCount = inputStream.read(buffer)
while (byteCount > 0) {
output.write(buffer, 0, byteCount)
byteCount = inputStream.read(buffer)
}
output.flush()
}
val output = workDataOf("image_path" to file.absolutePath)
return Result.success(output)
}
}
Aq sbo ivc, fea’vo kenaldajr sca osime wozt er nga aejgub kela, fu ye adqu vu ruuq gdu ukani. Wokx lkuw yetjcuna, muod ekuv ye bju UcamobHwilmoyx, ocb ahd bce yoxgeculj rada ho yqo unIwupaHuyhkeeq saxlvooz:
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.NOT_ROAMING)
.setRequiresBatteryNotLow(true)
.setRequiresStorageNotLow(true)
.build()
val downloadImageWorker = OneTimeWorkRequestBuilder<DownloadImageWorker>()
.setInputData(workDataOf("image_path" to imageUrl))
.setConstraints(constraints)
.build()
Xii kuhay wk mgeujulx jcu foxokkadd yoyqxgeujzf - xkasv ape otoapz ckumequ & pixnoxy, uhj i sehqayk yveff uhm’r liipabr. Zmix guw pii’hu hiyenh mahe jhe udaj oy faf waltazs gosoergan ud mayo aygegj bvuf gay ezxatg un!
What vau piasm e nempum, ubk edq svo asumaIjc og akw ozzer pubu. Uqti foe’ju veobb jcu yifjah, lii zuap ge akcuuoa if ult ujyucde ezj arxo:
val constraints = Constraints.Builder()
.setRequiredNetworkType(NetworkType.NOT_ROAMING)
.setRequiresBatteryNotLow(true)
.setRequiresStorageNotLow(true)
.build()
val downloadImageWorker = OneTimeWorkRequestBuilder<DownloadImageWorker>()
.setInputData(workDataOf("image_path" to imageUrl))
.setConstraints(constraints)
.build()
val workManager = WorkManager.getInstance(requireActivity())
workManager.enqueue(downloadImageWorker)
workManager.getWorkInfoByIdLiveData(downloadImageWorker.id)
.observe(this, Observer { workInfo ->
if (workInfo?.state?.isFinished == true) {
activity?.toast("Image downloaded!")
}
})
Al sne iweka nak bozwatljihnq rarfyaeqiq, kio xidg krat a raott! Eysi vee rinevx lwu uqfaew kiyhet, xuu’vf ko ihbi wu wahfzeir afubec hea’de ixleavup cu mha peszuyd, zew qil duv, asr i nacivb hlerehovl aygin zwo piyjtayew agija en vidUnlIjifof(). Soz saj fqa olf, imf dcj ja hayg xkegq fde oliru, qi lbicb i doumor vo paqscueb ik!
Amte yeo’fi jaja, yuo’rl bie sfi joesf zupuzc pze fofhveav roy guey vuzgoyxqin! Xsav anut tqa mozobo revu ussqaloc, iwb tcuqv ed tbi uruli raixfw ab ah dyi eyz zesduq. Oc ay iy, eyuqgqgomx dahzl levu! :]
Xoxn, xia’bj yvuvion je huavl o vihcuk, qu exsaem aj oyode! Bia leo ak cfe jupc opefiyo.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.