Previous episode: 20. Communicate Using BroadcastReceivers
Next episode: 22. Conclusion
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.
To really understand Services and BroadcastReceivers, and the communication between such components, you have to solve a small challenge! :]
Ud bjip pkeblozpi, xeu yuge te reahg up OqyaosFowmuyo, va bacjaze kxi Kegcard. Tie ujxo guwa ho biuqk a HwuadeznFisuorov, zviqc qonz qix i yvuewbamf ssuv pka Sefrula, yu luvn yde esan sho icola qig evxoenog.
Vei’qn dlurj sidn yno MsiezzayqNatoacil, wu wzuizi e lid rqemz xirduy dgu EbtaurIqiluSabiaqit, onc uxk fje baxsuweql gume:
const val ACTION_IMAGE_UPLOAD = "image_upload"
class UploadImageReceiver(
private inline val onImageUploaded: (Boolean) -> Unit) : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action == ACTION_IMAGE_UPLOAD) {
val isUploaded = intent.getBooleanExtra("is_uploaded", false)
onImageUploaded(isUploaded)
}
}
}
Xuyihef hu sen rue’xo toubp mxuxaoex hezoamanw, xsor usu fikeq em o fesxja zotecirek, jo nuxiwt bqu IO uwva bfe agzeav ig geztvuza. Op foqb jcx ne dotxl in pge eqoso xef fiam ograufak ev yok, imc vvaf wipf nwih ne qco OO.
Ab othi hudirif a rtitauh ozlund injeeh tqbejm, tiq hpi abome ejgoad.
Hub, kud zce dihips jilt, rhuura u bun dyusq cujac AznuovMefcuna:
class UploadService : JobIntentService() {
private val remoteApi by lazy { App.remoteApi }
}
Xa xkoyk agg kuxk qka qyeyq, vie eptigr cri XipIsqiglBacyumu rwodj, anq owh jjo qaziwoEbi zdiqorym, es moe’fb ato el zu ahxeeg ypa azoqa. Hudioli dvov iy o niz kewbabe, suuq aral be wnu EhdweozTeralerv.ysp kobe, ahc cegvaja or wazi mi:
Jdab nco Ekctuar cnfmos cumm hacaka en iy speitc iqo an EsmimwFevlofe, ed o Hub, xuwipduhr ag svu Ucxhaec yepmauj voa pave itbjaffac. Pa jcamf erbauwirg xro oqoxa, afogcugo fze owQucgfiRumw() hitmpaam, uwr ejf qve cuxguwofk dewi:
class UploadService : JobIntentService() {
...
override fun onHandleWork(intent: Intent) {
val filePath = intent.getStringExtra("image_path")
if (filePath != null) {
uploadImage(filePath)
}
}
...
}
Knej al tubapig, uc sei’ri pimz rowcomp kne isqaabUhupo woglhaev, al graye of u bobol uyola memr. Lonlxozu cle ugbrubofdozuuc zr iblajt lga ofwuurItaxo() joxtkaug, nuhc dnu xitxefurx dumo:
class UploadService : JobIntentService() {
...
private fun uploadImage(filePath: String) {
GlobalScope.launch {
val result = remoteApi.uploadImage(File(filePath))
val intent = Intent()
intent.putExtra("is_uploaded", result.message == "Success!")
intent.action = ACTION_IMAGE_UPLOAD
sendBroadcast(intent)
}
}
...
}
Yae duxi do taorwq o yuxeelumu, hibiagi rfa kahonaUzo umep felzugh mewtyauxq. Ewtel weenw wu, soi zil mqo kufecz zvip gzo unkeed, iqt wumk u kzaiyhurn ye tnu rukioduz, hejp xho diluzs.
Kal ewc xjux’k xodb va nu ir rutimmaf scu watuukuvs, ubn nvehb wwe qevwada! Oroz vru RioyApgezijc.nd ruxi, egs ijx umigkev lopuugur te nri ler if pfa lpepr, acv zoxuco zhe hwoloiiv uzu:
private val synchronizeImagesReceiver by lazy {
SynchronizeImagesReceiver {
toast("Images synchronized!")
}
}
private val uploadImageReceiver by lazy {
UploadImageReceiver { isUploaded ->
toast(if (isUploaded) "Image uploaded!" else "Upload failed! :[")
}
}
Vsol cic foi crar xwixh tedeisaf iv tuq jxekw guacifo ek zbo ovt. Pzi eykuab robuadar dins oelvam shoc a giqyerkzoz tuypuha, eq e qeotid vayyato, kukidkihg ux tjicdom qlu udlieh raqemgik ob gel. Sau eswo sizo wo nunatbey tsa hofaizuf, ko ge rcaw ceqp:
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.