Uhlelo lokusebenza
uhlu lokuhlola

    Oxhumana naye





    Ibhulogi yethu

    Sihlela ukubonakala kwakho! Ukusebenza okuhle nge-ONMA scout ukuthuthukiswa kohlelo lokusebenza lwe-android kuqinisekisiwe.

    Oxhumana naye
    ukuthuthukiswa kohlelo lokusebenza lwe-android

    Ibhulogi yethu


    Uhlelo lokusebenza lwe-Android – Uwakha Kanjani Umsebenzi we-Android Lifecycle Callback kanye neSettingsFragment

    Ukuhlela uhlelo lokusebenza lwe-Android kuwumsebenzi oyinselele kodwa onenzuzo enkulu ozokunikeza umkhawulo ngaphezu kwezimbangi zakho. Inqubo isekelwe eminyakeni yesipiliyoni ekuthuthukisweni kwesofthiwe futhi yakhelwe ngokuqondile izidingo zomkhiqizo wakho. Kulesi sihloko, sizochaza indlela yokudala i-Android Activity Lifecycle Callback kanye ne-SettingsFragment. Sizophinde sihlanganise indlela yokusebenzisa i-Java njengolimi lokuhlela lwe-Android. Ekugcineni, inqubo izokuthatha kusukela ekuqaleni kuya kumkhiqizo oqediwe.

    I-Java ulimi lokuhlela olukhethwa kuzinhlelo zokusebenza ze-Android

    I-Java ingolunye lwezilimi zokuhlela ezidume kakhulu ezisetshenziselwa ukuthuthukiswa kohlelo lokusebenza lwe-Android. Kunamakhulu ezinhlelo zokusebenza ku-Google Play Isitolo ezibhalwe nge-Java. Ulimi kulula ukulufunda futhi lunokukhulu, umphakathi osekelayo. Lokhu kukwenza kube ukukhetha okuhle konjiniyela abafuna ulimi olusheshayo noluthembekile lokudala izinhlelo zokusebenza zeselula. Ezinye zezinhlelo zokusebenza ezidume kakhulu ezakhiwe ku-Java zifaka i-Twitter neSpotify.

    I-Java inikeza isethi ecebile yama-API, njengokuhlahlela i-XML nokuxhumana kusizindalwazi. Futhi kuwulimi lwezinhlelo oluzimele, okusho ukuthi abathuthukisi ababhala ikhodi ye-Java bangayisebenzisa ku-Windows, I-Linux, noma i-Mac OS. Izinzuzo zokusebenzisa i-Java ekuthuthukisweni kohlelo lokusebenza lweselula zikwenza kube ukukhetha okuhle kakhulu konjiniyela beselula.

    I-Java ingolunye lwezilimi zokuhlela ezidume kakhulu zokuthuthukisa izinhlelo zokusebenza, ikakhulukazi kwabasaqalayo. Ulimi luphinde lusekelwe yi-Android Studio. Ngenxa yokuthandwa kwayo nokusetshenziswa kabanzi, I-Java ulimi lokuhlela olukhethwayo lokuthuthukisa izinhlelo zokusebenza ze-Android. Nokho, kunezinzuzo zokusebenzisa ezinye izilimi, njengoKotlin, ngokuthuthukiswa kohlelo lokusebenza lwe-Android.

    I-Java iwulimi olugxile entweni olwakhiwe yi-Sun Microsystems in 1995. Inezici zokulawula inkumbulo eziqinile futhi iyafana. Iphinde isekele umqoqi kadoti ukuphatha inkumbulo ngekhodi, okwenza kube lula ukuphatha inkumbulo. Lokhu kusho ukuthi ikhodi ye-Java ingaba yinde futhi ibe yinkimbinkimbi kunekhodi ye-Kotlin.

    Ngenxa yokuguquguquka kwayo nokuqina kwayo, I-Java iyisinqumo esihle kakhulu sokuthuthukiswa kohlelo lokusebenza lwe-Android. Ulimi lufundeka kalula futhi lusebenzisa imitapo yolwazi evulekile eyenza inqubo ibe lula. Izinhlelo zokusebenza ze-Java ziyakwazi ukusekela izinqubo eziningi, okubalulekile ezinkampanini ezinezidingo ezinzima. Bangakwazi futhi ukuphatha inani elikhulu labasebenzisi.

    Enye indlela yokuthuthukisa izinhlelo zokusebenza ze-Android iCorona. I-Corona ifundeka kalula kune-Java futhi isebenzisa ulimi lwe-LUA. Iphinde inikeze nge-SDK eyenza ukubhala ngekhodi kube lula. Inezinzuzo eziningi, njengokusebenzisana nayo yonke imitapo yolwazi. Ingase futhi isetshenziselwe ukushicilela izinhlelo zokusebenza kwezinye izinkundla. ICorona isetshenziswa kakhulu ukwenza imidlalo. Ikhodi ifakwe kumhleli wombhalo futhi ingasebenza kuma-emulators ngaphandle kokuhlanganiswa.

    Ukwakha uhlelo lokusebenza lwe-Android kudinga i-developmentsumgebung

    I-developmentsumgebung imvelo ekuvumela ukuthi uthuthukise izinhlelo zokusebenza zamadivayisi we-Android. Kukusiza ukuthi usethe uhlelo lwakho lokusebenza ukuthi lusebenze kahle kuwo wonke amadivayisi we-Android. Ngokwesibonelo, uzofuna ukudala iphrojekthi ekuvumela ukuthi usebenze ngezinsiza ezahlukene kumadivayisi ahlukene. Iphrojekthi kumele futhi kube lula ukuyizulazula futhi ibe nendawo ehlanzekile nehlelekile. Kufanele futhi ikuvumele ukuthi uthuthukise uhlelo lwakho lokusebenza ngaphandle kwezinkinga.

    Imvelo ye-Android idinga ukuthi onjiniyela basebenzise amafayela e-XML ukuze bachaze amayunithi ezinhlamvu e-UI. Amafayela e-XML angachaza amamenyu, izitayela, imibala, kanye nama-animation. Lawa mafayela aphinde achaze ukwakheka komsebenzisi obonakalayo. Ngokusebenzisa amafayela e-XML, ungalungiselela uhlelo lwakho lokusebenza ukuthi lusebenze kumadivayisi ahlukahlukene kanye nezinqumo zokubonisa. Ungaphinda uchaze amanye amafayela esisetshenziswa kuphrojekthi yakho. Ngale ndlela, uzoba nokuguquguquka okwengeziwe esikhathini esizayo.

    Idala i-Android Activity Lifecycle Callback

    Indlela yomjikelezo wempilo yomsebenzi we-Android isetshenziselwa ukuthola ulwazi mayelana nesimo somsebenzi, njengesimo sayo samanje. Kwezinye izimo, indlela yomjikelezo wempilo isetshenziswa ngaphambi kokuthi umsebenzi ucekelwe phansi. Ukuze ubone umphumela wale ndlela, ungasebenzisa i-logcat. Ikubonisa okukhiphayo kusifanisi, idivayisi, noma kokubili. Ungakwazi futhi ukubona okuqukethwe ku-logcat kwe-onCresume, ekuPhumeni, kanye nezindlela ze-onStop.

    Uma umsebenzi uqalwa kabusha, isistimu izobiza i-onResume() shaya ucingo futhi. Kufanele usebenzise lesi senzakalo ukuze ugcine isimo enkumbulweni, noma ngabe umsebenzi wakho ubumisiwe. Ngale ndlela, abasebenzisi bakho bazokwazi ukufinyelela ekusebenzeni kohlelo lwakho lokusebenza ngenkathi umsebenzi usamisiwe.

    Indlela ye-callback ye-lifecycle ingasetshenziswa futhi ukuphatha inguquko phakathi kwezimo ezihlukene zomsebenzi. Ngokwesibonelo, isidlali sevidiyo esisakazayo singamisa isikhashana futhi siqalise kabusha ividiyo lapho umsebenzisi eshintsha izinhlelo zokusebenza. Ingase futhi inqamule uxhumano lwayo lwenethiwekhi lapho umsebenzisi eshintsha izinhlelo zokusebenza. Futhi, uma umsebenzisi ebuya, ingaqalisa kabusha ividiyo isendaweni efanayo neyishiye.

    Uma umsebenzi usudaliwe, izodlula ku-OnCreate() kanye ne-Destroy() izindlela. Lezi zindlela zizobizwa kanye kuphela phakathi nomjikelezo wokuphila womsebenzi. Nokho, uma umsebenzisi evala uhlelo lokusebenza ngaphambi kokuthi umsebenzi uqedele, i-onSaveInstanceState() callback izobizwa.

    Ngaphandle kokudala umsebenzi, ungasebenzisa futhi i-onStart() indlela yokuqalisa kabusha umsebenzi. Le ndlela ibizwa ngesistimu ye-Android ngemuva kokuthi idale umsebenzi. Futhi, ngemva kokuba umsebenzi umisiwe, ingaqalwa kabusha ngokubiza ukuqala kabusha. Lokhu kungasiza isistimu ukuthi igcine ezinye izinqubo ezingase zisebenze kamuva, ngaleyo ndlela kuthuthukisa ukusebenza okuphelele kohlelo lokusebenza. Nokho, uzofuna ukucabangela imininingwane embalwa ngaphambi kokusebenzisa le nqubo.

    Isinyathelo sokuqala sokudala i-Android Activity Lifecycle Callback ukuqonda ukuthi ama-callback asebenza kanjani nokuthi acelwa nini.. Eyokuqala ibizwa nge-Dala(). Lapho le ndlela isetshenziswa, umsebenzi uyadalwa futhi udala yonke imibono edingekayo, izibopho, kanye nezinhlu. Ngemuva kwe-OnCreate() shaya ucingo futhi, i-OS izodlulisela ukulawula ku-OnResume() noma onDestroy().

    Ukudala i-Android SettingsFragment

    Lapho wakha uhlelo lokusebenza lwe-Android, ungasebenzisa i-PreferenceFragment ukwenza ikhasi lezilungiselelo libukeke lilihle futhi lifana. Lokhu kuzoqinisekisa ukuthi abasebenzisi bakho banokuhlangenwe nakho komsebenzisi okungaguquki kungakhathaliseki ukuthi yiziphi izilungiselelo abazibhekayo. Ukusebenzisa lolu hlobo lwengxenye, kufanele wandise isigaba se-PreferenceActivity. Khona-ke, kufanele usebenzise i-onBuildHeaders() shaya ucingo futhi.

    Ungakha futhi Izingcezu ezikhethekile. Lezi zingcezu ziyisakhiwo esivumelana nezimo kakhulu kunomsebenzi wakho ojwayelekile. Izingcezwana ziyizigaba ezijwayelekile zomsebenzi wakho, futhi babe nomjikelezo wabo wempilo. Baphinde bathole eyabo imicimbi yokufaka. Ngaphezu kwalokho, ungangeza izingcezu kuhlelo lwakho lokusebenza ngenkathi lusebenza.

    I-PreferenceFragment iyingxenye enohlu lwezinto ezithandwayo. Isetshenziswa ezinhlelweni zokusebenza ze-Android futhi ilondoloza izilungiselelo ezithandwayo kokuthi SharedPreferences. Ayisekeli itimu yedizayini ebalulekile, nokho. Kungenzeka ukunweba i-DialogPreference ne-TwoStatePreference ngokusebenzisa izilungiselelo ze-API.

    Uma uhlelo lwakho lokusebenza luhloselwe ukwenziwa lube ngelakho kakhulu, ungasebenzisa i-PreferenceFragment. Leli klasi linconyelwe i-Android 3.0 nangaphezulu. Ikuvumela ukuthi wenze ngendlela oyifisayo ukubukeka nomuzwa wohlelo lwakho lokusebenza. Ungakha isixhumi esibonakalayo somsebenzisi sohlelo lwakho lokusebenza. Isakhiwo sibuye sibe ngokwezifiso kakhulu.

    I-PreferenceFragment iyindlela elula yokugcina izintandokazi zomsebenzisi. Uma ushintsha okuncamelayo kuhlelo lwakho lokusebenza, I-Android izogcina ngokuzenzakalelayo izinguquko kufayela le-SharedPreferences. Kodwa lokhu kusho ikhodi eyengeziwe yokusingatha izinguquko. Izinhlelo zokusebenza eziningi zidinga ukulalela izinguquko kufayela le-SharedPreferences.

    ividiyo yethu
    Thola isilinganiso samahhala