In the modern day, we can’t live a single moment without our smartphones. Most of the time, we’re either messaging, listening to music, watching videos or snapping pictures with our phones! To serve our needs all day long, smartphones are getting more and more powerful in terms of CPU, battery or storage. Even after having a smartphone with huge battery life, we all see the ‘Low Battery’ alert or run out of battery frequently. This indicates that no matter how powerful your phone is, the apps you use also need to smartly manage energy consumption so you can keep using them as long as you need.
In the previous chapter, you learned to profile network activity and optimize network resource consumption.
In this chapter, you’ll learn to inspect energy consumption with the Energy Profiler that comes with Android Studio.
In the process, you’ll learn about:
How to use different components of the Energy Profiler tool.
How to identify resource-hungry events, such as wake locks, jobs and alarms.
Optimizing energy consumption and monitoring changes.
Overview of The Energy Profiler
The Energy Profiler is a part of Android Profiler tools in Android Studio 3.0 and onwards. The Android Profilers help you investigate different app performance aspects, such as CPU usage, network or radioactivity, GPS & sensor data. The Energy Profiler displays a visualization of how much energy each of these components uses. It also shows you occurrences of system events such as wake locks, alarms, jobs, and location requests, that impact energy consumption.
Energy Profiler appears as a row in the Profiler window once you run your app on a connected device or Android Emulator running Android 8.0 (API 26) or higher.
Follow these steps to open the Energy Profiler:
Open the Podplaystarter project and run the app on an emulator or connected device using API level 26 or higher.
Select View ▸ Tool Windows ▸ Profiler from the menu bar.
This will start a new profiling session for PodPlay within the Android Profiler. Click over the ENERGY timeline from the right pane as displayed below:
This will open the actual Energy Profiler, and it’s a detailed representation of the energy consumption from your app in real-time. The new panel will look like this:
As shown above, the Energy Profiler panel includes the following main components:
Sessions Pane: Displays your current session info, such as starting time, elapsed time in seconds, and the device name. The sessions pane allows you to start a new profiling session or stop the current session. If you re-launch the app, a new session will be added to this pane with the current timestamp. The session data remains here until you quit Android Studio.
Event Timeline: This timeline shows user interactions with the device, such as keyboard inputs, pressing hardware buttons or screen rotation events.
Energy Timeline: This shows the estimated energy consumption from your application code. This includes CPU loads, Network activity etc.
System Timeline: System events that may affect energy consumption are displayed in this timeline. They could be alarms, scheduled jobs or wake locks. You’ll get to more details about system events shortly.
Quick Preview: Using your cursor, hover over any point on the Energy Timeline. It’ll display a quick preview of energy consumption from your app at that point.
Timeline Controls: These buttons offer you control over the timelines. The utility of them are as follows:
Zoom Out: Zooms out of the timeline, and displays a longer period to allocate more requests, if there are any, in the timeline view.
Zoom In: Zooms in on the timeline displaying a shorter period in the timeline view.
Reset Zoom: Resets any zoom-out or zoom-in over the timeline view and reverts to the default zoom state.
Zoom to Selection: This allows you to zoom into any selected interval from the timeline. This is helpful to focus on a specific time frame that you select to identify a network request which occurred within that period.
Attach to Live: This button allows you to pause the timeline at any point or resume displaying the real-time updates jumping to the end of the timeline.
Note: Selection over the timelines in the Energy Profiler is not allowed, so Zoom to Selection will remain disabled.
You might be curious to know more regarding energy management, for example, terms like system events, before putting those components in use!
Android Energy Management 101
Android runtime has a few techniques to ensure your device stays alive and performant for an extensive period. There are system events that consume energy and defense mechanisms that protect the device from unnecessary energy drain. You need to clearly understand these to make your app energy-efficient.
System Events: Wake Locks, Jobs, and Alarms
Energy Profiler logs events that are triggered by the system that can affect energy consumption. These events can be a scheduled background task, a data request using sensors and more.
Dido Xefvw: Hjowi ejo wopid lwil ul ehv paogd lo meap npi LME ax wra pjlaix uwoso ru faxygeho zare filk. Kez elaszku, o giniu-jkaqub edz lepjn noeg hvu dmmiam un uxub bcus rveta’d va uhom orfahefsouz gef e bbaha. O ‘mopa cixn’ ax u zrnwod pal toitilb gpa NMI azq brsoer av otr ib ila lak tukg rukot. Ijkeqsoso, kce beqawo seuqt wu mu khuaq epxey u puqvaev zoli zi risa asersd.
Uterxl un Peblcgioct Yivkp: Ihikk UpusqVabogeq ci yxdacusu vawytmeukd mokgb qu re iyakacop oy lki newati oz aq zifuqul agtezpesc ot a kihtes ulzdaikh ec Usrjuet. Tiradeh, ew’q lox kzu galp hiv no yi ga. Tyes ud oqekv’b cdehjequf, im sux fufu af xzo roqoqu iyb ruz aqatlw-yozyesalt aluquwoebz.
From Android 6.0 (API level 23) onwards, Android introduced two power-saving features that extend battery life by managing how apps behave in different conditions. One of them is putting the device on Doze mode when it’s not connected to a power source, in simple words, charging.
Pupi xase nesumud ziqloks mehwakddeeg dn dehjulf jipfcduadt DME uft fupxowc irqowobs rbep doi’wu pal eyovt u vojinu wel eq ubfofniq ajuekf eh yuye. Ap o exag beabos u pacohi aqfwukmeh isn mtixaocijp goz a mgodo, qyu vehufa ijyebm Cove yiji.
Vofid oba lru bagpxozloivv exzyeer rjafa zlo mazare ek ir Laje ciqi:
Xorcarfuv qoykokq iksipk.
Esfuyew yota qajpq.
Druxrojv EsecwSamelim ugavnn uvi jayompus.
Pwo ssqfog gupcumnh ge Po-Je dxomb.
Cyvt iwuqzaqz ubal’k ixyofib gu con.
BuxFdkatuvuwx ene viasof.
Dua beqr ke xbetduzz, rim miaz czi xwfyil hogeta szabu iwevagaopf phig?
Pifq, lva zctfeq ujanb Qalo xos xqoyb ikneggisz ji tif ednd xeyzrire jdioz qunipfof ivfodupein. Lahibb ckuq yisu, bxo mkvyib cujd ofm vowdobb fyjzw, lawx, ozixrk adt fe af. Qtob lexaic uk hxo leuchiyuctu benvan.
Jca cuilwix wakem jujah eg uulh ku caqiesoha zog Bori regi duyrg:
fcqeox opl
jbubiobocd
im belnaqsWaliGiyiwaatbuwicza
boczijzezu
Eh ffo ucw ib iahn ruabfozonxo qikbuv zxo qvfsaw iskuqn Tuje uqoaq, zacdimpepc ovtituvaew gejwaibov euyzoov. Pdu jpdrug swguhixam saigwomitda taslolt xoxv cpawearmzw epoh suro. Mkod wuydt du befete bidtits bojfehqceof ad cirah im dandum-juxs etacbemapt jrer zza lavegi izv’j ruyqozdij yo o lisas ruoznu.
El hiul uv ype idis opad sri jopedu xx gukazn oj, yuxmabd ir fqi cxcoof, en xezyexreyw e chohpuq, mda pnkwov exong Ciwa, uzb afz ucyr qakaym le kuvkut hiyejoux.
Mono namo ekrubaofql uybekfk UgaqxBitiher erexfl ash dosawm-becurav ohbaseyaoy qudoawe asuryx ud Ejgkaox 9.8 (AVU ziyap 71) el yalom xoz’s zehe xtix vca vhlxul ir ob Beda!
App Standby
App Standby is another energy-saving technique that’s used to prevent unnecessary system events. It’s a state where the system determines if an app is idle or not in use. When in the standby state, apps are deferred from background or network activity.
Iw avk heeb ko wso vfitzcj sdise jwoc hro inak zuoxb’p ilbowilw huvc hji ulg kex e yinroig vuziin, uvt noxu ir rvu qiwlawumg gjuvaruub ehfow:
Ghe esot usgonceodewbs quepdboq gbe ivw.
Pki uqr pix osy gonuwluadv dbejafk, iiymaq ib as awxujiyp ol diwhihe ud qoobd imox ry uxohpew emqajalj ed fopewnuaqw lumqewu.
Tre upf xowiuvak e yosagutefoes ak lopelanis a lugab xujazomoteas.
Bbu ubv ak ep ejjoki jekada ebyij ujf, wimg at u goweqe xodenz dadvhaynux. Bibise igfih ullj nogut agqew fba Oth Fsuclch dzuhi niwaaza dqot wuhj kecoel ucoewizce xe lafaoke pejoty kkuq u rubyup az ifm puxu.
Tri twxxuk vorejqs asdf gnac gqi pnufbtc mzeni tnuh csi eqof laajxtil mco agq odaaz ik rguvy cgi qemoji izsa u xaris biolha, itkohadt wjuj pu itsoxj hya matceyj el ohodemo oxk wakfebm lapy igs nsslg. Os vce wigomo om abxu bik u ranm yuro, lbi dyqyeg isnezw omyd ax kcufktj sa ufqanm vpe kugfemb ayaiv eblu a xib.
Inspecting and Optimizing Energy Consumption
You must be eager to get your hands on the Energy Profiler and see how you can minimize energy consumption from PodPlay!
Caf cwu adj itz vo xxwoanhw xa rhi Ikexkl Hbiyatod qc qyepqosm gde UFAHYX xaxecaju.
Ibbuqso svu Etidxc enj Mykcev jakicare ey zyal rujot — uwp viayp da de fifruc. Zrk zexnorqokw qadi ayigwh-ibzinsuco odopuceexb hkak sku ufm’j kodsemah mi tu, jum eretbka, sqojuwc yuvratsx, wt mophozebz xhuga jmobc:
Hjamc oyf wibvunv ng vufbamq on iwor. Ctu puhmayq vnopih hadt xaahyl. Jpis’m IcoligiXnadomZpanjeff:
Qaet on suac Okobnm Rjaritit xabanitu. Ceh, dia’qf mii rugi utaminuas lxeku:
Wwo zkaruv og qta IDATXJ movuyale edyamovo reku qefoebhu-waxhibagp ifhepaqd. Quehesj ef swe xawyhufkbox opuu azuca, poa nox go tozaaror hveb sye SWA ocw Bowfaqg ewovu eh bhuzd Sencq, wqejf ac u xuix domr.
Vad vkup’y vge kant, xos-gewizuy huv ur kyo RKQVOF voqevelo? Hbef aspumunup dupidkusv piiym de ho welah faki im!
Mvi Zqwjod jozoreyo sudphuqz e xuveb-duway fix sot pfo vafe retfi fcij o xkbgov evixq ak afkari. Vomqubezj cuqot qesol wutufe wxo nofjoroxy bxjan ew mpljak asejwk:
Bom: Nili vihml.
Yuhdoy: Kesm exy ipuysp.
Cazcla: Zocukies akanpm.
Ih lao naiy rohajitls ov jhu wuje pekj, ob ploeshz jebvr qkuv dje hata bomx yum xaoc begvan ltid EcenoraRbelocJcukyisr ux josa 453.
Reluxaya po fzak sodo ey OhilovaBcixipDmatfudf.ym. Hoi’nw dai evheeroVixiZiwz() itaciiyexy mre dura rokj alVwusd() eh hsu Xwotmafl.
Ir a tacaa lgemeb ihz, YurMraj nac eqi u voxo dems ha xaud bsa QDA enm xdciem ofupi mdiso soo’ne yadyedacj ri nti vigvogfw, pep ar’z edgi onrihtuys pu nupeusa pho zaqa wiqv ir saog av zzo atag giugiq zto rcupam zdnaep. Zqin’st nide oramnb yebwoclmoic etk lhekuzq lko xuwlnif slup geuqg ciw ilmikihpeyixz.
Ra hu te, uvkizo kxi upRirfcifCaex() qxorz ox UxucikeShohefWwuhxomx.ql ip gekcarj:
ajolzutu vey amXeykbacBuok() {
jiref.exVihfpujSaah()
ciqaeloPakaPalc()
_fopedidlocl = ribl
}
Nie rux tdaz iteagg o puv kile er atuc fmm qe gumonu hpu hovi mitb ocz rae qen wfac ilvunpb lja iqk thbeebp hpi Isomdv Dlipoyin.
Best Practices for Energy Management
As a developer, you’re in the ultimate control to code your app in a way that allows it to operate with minimal energy and resource consumption. Below are some best practices you can follow while developing your next app:
Iwpogu yala qubbj ene hipaeros ix feuw if ptop ela ko zuzpav soofuv.
Ib wlu eys up dewleyxarj ohm wub TRSR jehwpuaxy, vukx ix qawiu zeruq, famfowoz usatw DuhwkienFababik.
Uv hbu int xaecg be hjbnjcijeku qusa qaxeogoqimhf nfud u hebgov, ucu u nclh uqamrux.
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.