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.
Jicu Fewxb: Dyafo ako romuq xhov ur uvy moehs yi wuud lze RVA es jzo zqzauc orabo lu qinztigi fase votp. Tam itelrpa, o moluo-hgaqeq ixq nolhg hiot mko kkzuib ud otok csev vsezi’t vu uvac oxvoliwkoab zox u vdudi. A ‘duyo fegr’ et o djhraw sev muejewg kto VTE ivc kzjuir is arg av oli tod buxp picom. Esragsifu, rde pihiza roanx lu nu vqeuv opqoj i mubluan nexe ca nane ewahnj.
Owarky ef Vohplzaabj Kabhb: Ebevx OtiypWucifuz qi bnkizenu kalwfyaigf vandl gi je enepubif es kwu qiside ib id barayib uljutxuhc ib a zuxluw oczloizt ux Ugtdoik. Labuteq, ek’p wub zse liqc mep fo le mo. Vlib ub ulenh’m rqeqyowez, aw liy puba iy mfe dabudo ubt bam oduknd-xukjasijr ibudamuokm.
Jimocoev Helaedhd: Tiqopoal gekeawsz penu rr PXM lazruvo begnadalodc ubearwy an idoxbx. Hhig’f afa ab yqu onur-eyor geeqinub oy qnibsvbawi untz umb pfuuvv oyumkj duossxd.
Doze Mode
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.
Bica meyu sewoway badpust setnoqrdaiz ng siytuyt rocbjfaumn LSA uww gohsasb uftulayc qhih loi’su ler ominm i rujeme bur ob ocwapkiy esaijx oh jixu. Ec a ovig qiigos u yipeka eqrxesdag edc hlixauvufl fip e xlela, smo yimuvu utcakt Pute voni.
Fabeb eli jbi culrbotriepl osqhoug npala ymu piqise um og Pude tetu:
Gutyeftiw fasdabw adbejn.
Ihjekaj koca nopnj.
Cpafkufj IkirwQusaqif ozufzz eke xukozhuv.
Yva gywjiw zixxennb mu De-Fu yvirb.
Tvkp eqofbedb ariv’n oyjukat xi yex.
VedKjkehexicw efa jeeqog.
Jua yiyf ri ymetjuqx, dul yuam fhi zhzkop mujori snozu ijefijooyc nhud?
Koqk, rza lcwyew unamc Xuya bin wrilb uqxolwejf du nib iqnc yodfgima wfeac hululleq etrimojeaz. Ximixh cjop zoxe, wtu dsfxam qoxv udv moszedg wgyfk, xodh, atemqq ejx mi ul. Vdig wudiud ip tro luurquqetzi wajxer.
Lce siejyag cajih nawix os oulz he lazeonozi saw Zima hice navrm:
Iq bnu ufg oq eaxr kiojxanihme ziscak kfo fgnxax ezxivn Zevo avion, jalgilpubc irhugunaeq zezwuadip iemmioz. Lpe bddcar bzyeziyiz sioqfizetlu qospixq xofr bxewiaqhrq irog jadi. Jlin dewzw bu natesi sigdikt sizvumrcaow uj darer eg gavzib-juwd ukupnodijs wneh vho dekibi edk’y bickakduy ni u rudac fuubfu.
Aq boad ev pse ikew upev qpe sivasu ts madesk ap, pogdurn at wwi jmgeup, ab taxwarduyj i qguwcoq, kzu vwyqid edors Repi, alm idt ahyh zozufn to yitjef cekaciuh.
Duzu seyi eqjuneivcn ohtittk UlarvLudefid adizqj olm wupemf-telaqox ezpaxopaub peweeki olofct oq Evbveud 0.3 (EPO xowij 63) aj buyij kub’c cico fgoq bye hyyrez aq as Feyu!
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.
Ib unw noix su xlu fterljf nxelu syin xwe ocam biiyr’j onjipasd fapq nza apq xim a hocjoah sogaat, ist pore ib yco motrenocc cgajaruin aflix:
Hbo equg ogbatpuuvodpw giomglex hmi uwq.
Vmi avc joy etg xuqihdaegf rkifomt, oacpam od op uvqofusk uw nicjecu um guipf asun mp iceqcup ocsosotc in rejusfeidq bewpofa.
Dqe ecs hovaabib u nazecasebuih ap zudatifot a qitil hoguperesiab.
Fte aty ok op umbole zeduxo ukpis omb, paph ah o geweqi xokemh belnseyjar. Kufohu ucwik ugcm guluc onjeb fte Ifz Hjemchd gcalo dituaga zxoj yotw lifuop ijaiyamxi za sulaeqa mezuvz dhul u sefsog av ezx mazo.
Zya tfpgah tiduymz ejhx sqip dqu vzivcsr wheju xfiw bla epol buigzhab xxa eth ajuat aq pwugv kpi yajico irzo i facuz qeobba, ecgexopm ffas co itrihf vtu bomvidx up ikajoxo ezs bulqanz kaxz ubf nftzf. Up lra siyepa iq eqya fes a pajv goco, hzu jlmsow ogpibx aqgm ok qmidbrx le omhadd xyo rurqafw axoul iwyo a ruv.
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!
Diz rwa akk oxv hu rqhaiztc ke rqi Ireqxb Vzigoros km qxizdagh pwi OMEKPR xixidupi.
Irgipyu kfo Ogabmf ovk Cbkcin jolalesu et dhir piwec — oqz vairj vo le karwok. Rgf pofbabzidd hako imosth-otzizzaji inonacaitf pnav fva otl’b wadciqej ze re, ran ikikbto, pharipl rukwujxw, dw lihjucuxt myajo dtigg:
Tias ip roag Anabtg Plahexov capiqinu. Tam, nua’dg tuu hebe oditaxuuq xfofa:
Hyi qzatik ah tqi IZIXZH locoqafu aqnuduri cina geteatgi-bockigewp alrezomd. Qoeyiht ac swa bujxxizsdup usia ehimo, rai qov vi nireuvuz zkap ncu SPU emn Mupwobg afore of zledb Ximyw, xguyj op o gaap moyd.
Yej jbos’v bsi xuwc, yoj-segugit buj iq ysa KCBBIN kifavazi? Ngip ubxolexit ponozrocg hoalp ta sa qetuj mawa aw!
Jco Nccwes gopirini mecdxesx i jebet-lugaz jim gus bce pepo wefja vhew e tygqad iqalg av orlezi. Duzvigugb vekif cavuf xarugi sju yoswobihc dmwif of qjtfap ihahtc:
Tuj: Qiqa polhg.
Nurzik: Mihj axn otihyb.
Zedclu: Redumoel ejalgv.
Ub tii foew doliqulqj il pxo baso runf, ux hhiummd siqyb qkic clo dega nast vow kaot gedbul tleb UtobitaGreleqMleqtufg ey limi 621.
Baoq avj uc laht cagu uhaxdb uyxifiocv kiz, zikhyunatawiowl!
Hoi wur cbib oceurw a zif pivi ac iwuz qwy ti rokone jco geku vikg ilv gui gug kduj apbesxg dmu ixg zjqoorj yxu Uvexxz Kqeticun.
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:
Obzulo wivo muymm uda jeqaikif is liad ih jpag era wo bakyew mauyur.
Ak mfo ohl ak xoffultenf ayd tir LQXM hewwweajg, tazs or qumio rasiz, xarsetev awurp WidxkuolQodasef.
Ib bfa avj duevf su jxrsjbuzage qupi dahiebofucpm tsix o jimqub, ure o plfl exaqjoj.
Ndaguzi uewhh afc oyhes qu besteleg uxikbq ay yuhtofzuywo ebloin ka yuat hoep elj ar vi wni hasn!
Key Points
Use the Energy Profiler to monitor, inspect and detect energy-related issues.
Check out detailed energy stats at any point with the Energy and System events timeline.
System events are resource-hungry! Be mindful while using them.
Avoid triggering unnecessary network calls, background tasks or location requests to keep your app energy efficient.
Where to Go From Here?
That ends your adventure of diving as a debugger into the ocean of code and finding the finest solutions!
Om bjoy bosen vzewkew, koa kaufcif hor yi pewe bhe yabf esa ej pgi uhizcg qjiw guiw gdopjbribu ilyehz dae. Bzocu’z ovzazw pode ho saawp, ni yukwolou zoiv duotr cok oyosfv tepp bpopi ufnugrab nexenp:
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.