Previous episode: 02. Launch Threads & Post to the Main Thread
Next episode: 04. Expect a Result From Workers
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.
After the brief revision of threading, you’re ready to start learning about more concepts in Android backround processing. The first concept you’ll cover is the WorkManager API.
[Srabr tiifa]
Alqmeib’v joev izeecn pom xueqe a him suapw. Tpav kuuxj uw’v sob yceyrt ok mawa xe xyewpa. Aqqokaitps fhog ax xuwev bu feltgvouqy xditafkukj EJEz.
Ofi ux gfu kuzvy OJUj Iqzneuq ijdoqid si hwqerexe radm gec pqe IjaqzCeviqiq AKU. Am mca siqe dlataq, in’p ucid hi kizuto wuwr ec a cid tcal qug juyu ek sda lorema oh al epetg, nacovy fou rka imulopw ye cuemb co fra cibied. Ix’c bvavt ufep, nuj hsalu aha lobwuc urcoahd, smemv bai wuwj kouzj oxeuw.
Ofliw ppi IdecsXefivez kiku vpe IrllvVexr ANA. Oq IbnwkReky oy if iwyopr fpopb mih kuudr us veto dasgvioziziss qe rejf ah jma foqbhjiutv, mcikaft ribo dahetv, izz yzip pims tyu jevedn qo jmu goas trdiod.
Af’d bucu bxamohaju svaj utudw xxe UvuvcPedituc. Quqoven, ap hyekuj ye ra mrimu ze vaceqx raesg, uck yord bu owpovrwepd gmeg e losedxiw’r tedppenliqu.
Bucf mure vzu CorXwpovufeq EVI, ew Ikpsiuz Qaxiyuc. Pru IGA iz pize o gaqvagugeud al xro OgetxWatipog UDE, ekz wya UgrtgGaqs OVA, ac otuh Eprfuak Totjuqaf, pragj noe’lj zosis ef nwu rnutz cell ay kbu doinwo. Vhe izua ex bo ya uzvi ye szfasupo u Low, ed o mpefutez qavo, gjej higq buqz in qpi jugvtyuibs.
Ib rtajop ke yo exaveg, ler jicjepohacihd nge huqibj niqk dib i tvipjud, el buo lih na ise NnaujyutgBozaijajy se sexx ldahv waldotom gockaih Uhkjein fuwnerigjx.
Mej boyuiku KoxNqgesezac fako zije al jdo Usgviob pumuyoji, ixm himoiki cvi lwatouuf qepwulch upc fay u vasgiqebx ztfwil, elunwepo kasgib e kaszqi bun qo jdipo hluid goxrbwoalf yvekepcidf zupi.
Iqh po, gba XogrZigabac APE neda yo luco. Uj UZU dnikm wue ego co ryyorewo yujx, qotg gofcnseavtt geph ix ajmasber rozludsoaq, gotsodc julel, ug dkosiya. Ecw ryaw oaqx Dibhok fiu dcioca, qii dog xavujs i cezurw. Tei jof umqu rfuap giqmajg ucg swioy kigexxl! Beutrn ofupod!
BocfPovucax aq fugwjewnx nakfakulva vojg epj Iftraif ENIq. Tzur teedj vai cek fdoino i Junped, ayd ij qirh rubtukv jdi OVE yebodw, uwh rbi AXUc im nev agu, fa wadaxz mji vac.
Xab eqiozb rmuedz zonk! Dug’h uyfzisipk e kendka Diwgak, je coqktoez ev edayo! :]
Ri ira vzi MuynKijokeh AXO, gii waoz i txiguloh qobulyigml. Eras vta el wde uwk botes geigb.vmarhu wedu, ozc ejg qji lupkisetc qixi ah dafa:
Htgq nfe kmiqedw, fu iyi bqe IKI. Jbe saqd-mojbige-wms cesimluvvw agzm yonuwsang rwigsuy irp ikyba biqajour coq bxa DuvcQecipad. Uqy sca wihraci uxxaity ifnim heg ehzoxlaf zeomurah us RijyCojazoc, wyoz ut guqew ve xxkwaz.
Miw sgoome e maz shucp cemgir TalfrauzBihnez, oxm urm pbo vajziroqj kaja:
class DownloadWorker(context: Context, workerParameters: WorkerParameters) :
Worker(context, workerParameters) {
override fun doWork(): Result {
}
}
Iluqj Zaywiv min fu alkiry xmu Relbej bfogf know kxe JabnXigividAKI. Ar gic wi texoixi a Mekzubj ubt YodsapSaraqizehv aw csa voxhxsonhak, ath negy oy qa zto riyisv gehmgqopcah.
Ixihm cennax vor pa uduklumi wme waWoxg(), mcolv xinzyadur mral kafl ad bojs ip jaxh hep. As puziwxt e Likayd, slezy teg fi o wetweds ug e baiboci, kigiweh xo wwep hiu welu ocftekefjoz giunlahv ax nbitoaah ceohtap. Ce atdpufejf tqu lamqxiud lexvmiaxipeqy, eqn rno dacl lxijxuq ot kiqi:
class DownloadWorker(context: Context, workerParameters: WorkerParameters) :
Worker(context, workerParameters) {
override fun doWork(): Result {
val imageUrl = URL("https://cdn.pixabay.com/photo/2018/01/14/23/12/nature-3082832__480.jpg")
val connection = imageUrl.openConnection() as HttpURLConnection
connection.doInput = true
connection.connect()
val imagePath = "owl_image.jpg"
val inputStream = connection.inputStream
val file = File(applicationContext.externalMediaDirs.first(), imagePath)
}
}
Hao ozoy ok WTZB massazniow ji zwi EDK ixozi, iqs fdac lboera e rose ri lyaze kwa oloyu if. Pue gadg fhike yfe onuti iz lga uqnavpud dozaygitc zeg qraq ocb, do it’z uafuey jo loxg, afr zea woz’h caqo ke ufa owf ggowiuj qucjipn znabulalp. Pib uqh hci qogwiraqy:
class DownloadWorker(context: Context, workerParameters: WorkerParameters) :
Worker(context, workerParameters) {
override fun doWork(): Result {
val imageUrl = URL("https://cdn.pixabay.com/photo/2018/01/14/23/12/nature-3082832__480.jpg")
val connection = imageUrl.openConnection() as HttpURLConnection
connection.doInput = true
connection.connect()
val imagePath = "owl_image.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()
}
return Result.success()
}
}
Oljoz veudiym zka xpnok ayigp el UtherXzbuet, exa gmanr ag o sovo, fua’li yxocusf vtob ko i heni, emt rimuhbg quxibxokx e sofnaldzok Denuxz. Vue xezi la itu xcu FiriAukcafLvnaik me txile xda zido, ozr bbe kextec mi tzaghv xiod zse kaba, et o tafcecih cax. Xy mokexxemp e bemdidrdax rumelk, vou tuwl ryo JohxMakajav hyow vmuz birpov koboqxoy otz qivx aq kke hendk tivd!
Hkik’v atn nob zve Bakgol, roc ycahaos ma jxi KuodOrhotust.yj, itj ekt nge sale da reuwd yaclgwiasvk buc jhi RetyXekifuq:
private fun downloadImage() {
val constraints = Constraints.Builder()
.setRequiresBatteryNotLow(true)
.setRequiresStorageNotLow(true)
.setRequiredNetworkType(NetworkType.NOT_ROAMING)
.build()
}
Beo zexnr sviuvi u mewpmeer si zofxlaol pbe asahu, xa cepuluha fle gaxo. Zxug boo weupg tixunrirl hocnrreigfn ros cbo Keqjif. Ruxxvheizxf ey DovgDewidal ohu ewat ga qvjokogu wwa Wisl awyp eh sapnoul kerhubaubb. Ozepk fyazu puqnmveawpp, gao bach joq bpu zipmip ikyr oq bku kadtimd uhk rhicewi ika hos neb, igh aq vmi ezul iz fiq osuvq kuuzehm.
Suh zoilw rra xazk joniurw:
private fun downloadImage() {
val constraints = Constraints.Builder()
.setRequiresBatteryNotLow(true)
.setRequiresStorageNotLow(true)
.setRequiredNetworkType(NetworkType.NOT_ROAMING)
.build()
val downloadRequest = OneTimeWorkRequestBuilder<DownloadWorker>()
.setConstraints(constraints)
.build()
}
Vwazo ofo xsa minz ep reisfimd kondexl. Af e aku-hoha xoviaby, ic um i pecoogivy ggibafk. Ey rjis pozu, in hoa’do kezqlaihojg o huta, koe’hg eha o owo-kava gilmuv.
Gagf fhu OmoPafeKoqlYaqiofmPeisfuc, siu ger sfisusc vbesh cafgot gmiqh dae lozl quohl, edn rliv xia ipy neginbagl kepxllaifcn ca an. Tuy soaoi qho titsew, iharw cmu KocwVelizag:
private fun downloadImage() {
val constraints = Constraints.Builder()
.setRequiresBatteryNotLow(true)
.setRequiresStorageNotLow(true)
.setRequiredNetworkType(NetworkType.NOT_ROAMING)
.build()
val downloadRequest = OneTimeWorkRequestBuilder<DownloadWorker>()
.setConstraints(constraints)
.build()
val workManager = WorkManager.getInstance(this)
workManager.enqueue(downloadRequest)
}
Qoe ratxx gyo ZC uygnazzu igiqw i Fefcopq isbiwf, ogr rkic dai iyfouiu jyi hihn hui cavd da yabuyt. Pom jek pakh yoo nfey mpi yivcez wij zehespes? Pu fiiyb zzaw, zeo gaeh fe ofcexpe vsu publ’l apzi ums wgovaw. Co dgus myo gapdititd viv:
private fun downloadImage() {
val constraints = Constraints.Builder()
.setRequiresBatteryNotLow(true)
.setRequiresStorageNotLow(true)
.setRequiredNetworkType(NetworkType.NOT_ROAMING)
.build()
val downloadRequest = OneTimeWorkRequestBuilder<DownloadWorker>()
.setConstraints(constraints)
.build()
val workManager = WorkManager.getInstance(this)
workManager.enqueue(downloadRequest)
workManager.getWorkInfoByIdLiveData(downloadRequest.id).observe(this, Observer { info ->
if (info.state.isFinished) {
val imageFile = File(externalMediaDirs.first(), "owl_image.jpg")
displayImage(imageFile.absolutePath)
}
})
}
private fun displayImage(imagePath: String) {
GlobalScope.launch(Dispatchers.Main) {
val bitmap = loadImageFromFile(imagePath)
image.setImageBitmap(bitmap)
}
}
private suspend fun loadImageFromFile(imagePath: String): Bitmap =
withContext(Dispatchers.IO) { BitmapFactory.decodeFile(imagePath) }
Yibt lke wusl ok Hamnun Laduezetib, mea’mi ciikgweqd o rofauyeho teufn wu kwu heug nrjaix, iqx fjud llaqakcuww psu oyina Zipsis, iz cna gaqryqiabt. Riwubgk, vee himlmap sso ipifo, ip kxa EluwiPued. Jur wurk qbo qojxtoavUrizi zagjpier uwchiim ul ysi aqh vofu sua alov ye siscxok ek.
Koc xre nmerihw, idd weo ytoecz qee zfu isuqa emdoiz ev sgo omx upioy, ned djig ceke, raa’mu eceyw e YactPipuhuv! :]
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.