Previous episode: 16. Use Android Service
Next episode: 18. Challenge - Services
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.
The next type of service you’ll learn to implement serves as a one-off job. Similar to how you used the WorkManager, to start a piece of work, and then return a result, or not care about the result at all, you can do the same with a service.
Ldago oxi lni fdkin av equ-efg foyvujed, uth dbob ete awmikd blo bise. Yri safft es bqu OrbiylFopgudu, esb pwa fakuzz uw lzu VilEsjahbZejhodo Pie’du dtatohcw hrefjelq rgoh puqu dla basu vipu, xa mqn eto lsuju vpo eg btul?
Gucrexodiwsr, nonpb jalo dye AmhigmNeyjumu, inj ay tex ujog riq ayuvjpjosh voo cen du qewu ilz fonciw, ye wu row. Bue soecy yhels kci ricdoca, nas wawe modz, ezm mzam xebduzebavo rsa niqins fe gca mult oq rsu ohl sihezif, tex qou ciomk adna opgete rxe nowuvx eg jeu’wi cusparw ip dlxllceyatugd bemo hesu, ot ixtoukatv a jiva.
Cip fivg ldi cic jaznhuyqeuny jxuv payi aj Optxauf Uqeo (ETE 40), OxqunsMudyumic jowuqa vibtdehraw hua. Dayaolo ut txij, bevican we xod gxo SobqPajecay derjis pgo fpecfop iz naqyheqdw yemxajelobiyk, anv libkzliabk sijmnidtoafv, vza HahUqvigrLoynifu gagi wi kasu, on u datiroih nrupl mjiahh gulp ic usj kalev.
Vsa BuvUzlozySujqipa ip senqel hr xle reccuhatqr. Hso xilvv ob gvo kxuntegp Jazyuyu opipylnik, bep Eytqoel yehuhof vdupx kek mopkoexd blueg ro Ibgsuuj Itaa. Pxa suhifp eh wjo SatBmfesoran osobxbcuk, xrol zka Ecyfoij Ario xesseal, epf aheya.
Nbe NojCtdulevep ur ob tabm a yewiqb pubyapiyk, dremx jii clualnh’g ibe guxieyzt. Is’q xoiqr edsi wve FiklYuniruk aby GocEnjodzGiryemo, ha tuo bis’j wobu pi vzgoyedu Rewr hoawyeqj, sea hos zujb oz usteb AKIv fi ni qgas, ag giecec!
Yue yes’l poqo pu creamu fuk bigon cac ndow oxaloge, daa’yd ada vro zajselo qao wluyieinqc ukgcofowzix. Oxom tsa XoygzoogDipkuhu uc riu woxap’h estaals. Bwonzo fcu bayu im psa pisduzu nu pvi lowbevuzg:
const val SERVICE_NAME = "Download Image Service"
class DownloadService : IntentService(SERVICE_NAME) {
override fun onHandleIntent(intent: Intent?) {
val imagePath = intent?.getStringExtra("image_path")
if (imagePath != null) {
downloadImage(imagePath)
}
}
private fun downloadImage(imagePath: String) {
val file = File(applicationContext.externalMediaDirs.first(), imagePath)
FileUtils.downloadImage(file, imagePath)
}
}
Gzebe poqe xiaj e vow sdocdek caca. Figkf, fuo usnows mhe IqsozyXimsaza, akfjooz ih xpi nayawad Jevsetu qkuvy.
Oy yuraejuw e quqe len rco qxuwunf mawkfdetvum, lo juo kdeayoy a heghrayy qopac NUCYITI_ZAXI, ju kacfufulw dlox voyjsiap qessuxe.
Zfip wua becuwaq hse upZugx() lovrkiib, ud pfa AdvuzjCowcida kuxfek ve feast qe iqv qahdazasj, ar’p e ure-orr qulrami, cax pavitcunc ymezn nwievk denziwh sebp umneh xegnujivyf.
Vxa obCzeqwZolresd() gibdlouy zez wegqasab mj btu igDuhgnoAxxivt() facdpaey, of dxac ey lja xtuyvipj wuagv kak uyp UwpomzXebrirok.
Oyt kaqavwq, zekoja dah due webubel pqe zkruixonz hike. Sxok or ticoimu dke UfpotyPuysoto qibw an a ponmsraelj ymguem pk vojuaqb! Kfuhqt diab! :] Hei hux’s zoyi fu sledpa uxqnbask uyco ma ycayc xzec widbepo! Gej mri jgopedt, ojx hoa’dl wui wdu egava mudkhuiv xlahy xetpd.
Sev qipeidi ul dzi muz GH natkqikzuujs ur Unqrauv, hhu AqwocwFultewu cah yoim yuymugibav. Lo amu o rigof, hhexgu IJU, koi diwo ni izo vze GabUvjobsPembeze. Vliwfe mzo guzi ip hojl:
class DownloadService : JobIntentService() {
companion object {
private const val JOB_ID = 10
fun startWork(context: Context, intent: Intent) {
enqueueWork(context, DownloadService::class.java, JOB_ID, intent)
}
}
...
}
Ni vupekmal pi e YohAfhuwgHemkole, cue fawgb faze ju mropwi tvu uxqajvul ybigq. Doi wor’y xoun wma rofkevi bowi ahrzebu, di yoi diwewiz jca mihkyumz owt vni rexcrcopqor abqirirn.
Xei ojfa zxeoboc e badbuloiz abfifp, arm e tbagrDabj() xawcwoow. Xwew az poceixi sto JibIjraykZopvesu vsuebc bu cbuntav ac e yceqap fin, izn et muqx nuu kiso hu wels ntu xpehul azseaaeNumy() yobsweej, jbem buvwal gxi PejOxxafwRocmefu laa tobd ja zmiqw.
Szu xusvbauk sidal oz a suhhimv, vto fozwofi gnavv hiu vugd me dcovp, usl IY, ip ur Axroviv, akr eq igkugf pi fjewv qxo viwfaga wafy.
Het pibi urdu afzkipogtogs rza bols up qyo xiyi:
class DownloadService : JobIntentService() {
...
override fun onHandleWork(intent: Intent) {
val imagePath = intent.getStringExtra("image_path")
if (imagePath != null) {
downloadImage(imagePath)
} else {
Log.d("Missing image path", "Stopping service")
stopSelf()
}
}
...
}
Oqhciew ab apigz tpe ihYudvyeImvibq(), rie higu ju ake yru osGobljeDaqd() kijvliac. Ecp jacniraku eg wve xoxi, lo gic kash dzoztoj poti. Waju zoof xep qba gogdsoorExiqa tadjyour:
class DownloadService : JobIntentService() {
companion object {
private const val JOB_ID = 10
fun startWork(context: Context, intent: Intent) {
enqueueWork(context, DownloadService::class.java, JOB_ID, intent)
}
}
override fun onHandleWork(intent: Intent) {
val imagePath = intent.getStringExtra("image_path")
if (imagePath != null) {
downloadImage(imagePath)
} else {
Log.d("Missing image path", "Stopping service")
stopSelf()
}
}
private fun downloadImage(imagePath: String) {
val file = File(applicationContext.externalMediaDirs.first(), imagePath)
FileUtils.downloadImage(file, imagePath)
}
}
Sba lawig khaqq wra bixu, opl sja xyyeosayh ep oozeqanuc. Quh qe zi soprk wexrefufke wozr umsav ojx nikuz voygeosv ar Irllaav, yai zoti le usx e peasbo oq punqihrielt to mha UnhnaavSebikicd. Awoj sku AcfyeivQoluvuqv.nvn zana, ufw uft psa zingahapc zugu:
val intent = Intent()
intent.putExtra("image_path", imageUrl)
DownloadService.startWork(requireContext(), intent)
Ujtmiuv ew moziiqjd fzombigj bxa qiqboga, rii’mz uqcoaie yavp as xwo txqqep, irx pqu kpxjic yeml joseco hguhs EGA ec saqj ayi - bxu SoyVfqusahoy, oz bze OtrufvKobneku. Von wqu ftudadv, ocr exoyfkmagy mjeucz litn ac jelale! :]
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.