Battery Optimization for Android Apps - Devoxx14

  • CategorySoftware

  • View66968

  • 1. Battery Optimization forAndroid AppsMurat AYDINSTM#DV14 #BatteryOptimization @maydintr
2. About Me• Android Developer and blogger in his free time• Senior Software Developer at work• GDG Ankara• Author#DV14 #BatteryOptimization @maydintr 3. Why Battery is Important?• Limited and main resource for device to function• Device may be needed for an important task when thebattery is running out of power#DV14 #BatteryOptimization @maydintr 4. Why Battery is Important?• Part of user experience• Bad user reviews and ratings#DV14 #BatteryOptimization @maydintr 5. What to do?• Buy extended batteries• Buy case batteries• Carry external batteriesTrontium-reactor can charge phones 50 times-299$• Carry solar charges#DV14 #BatteryOptimization @maydintr 6. What consumes the most?Derived from Jeff Sharkey’s presentation#DV14 #BatteryOptimization @maydintr 7. Usage MeasurementsFor a 1150mAh battery:• Watching YouTube videos: 340mA 1150/340=3.4 hours• Browsing 3G web: 225mA 1150/225=5 hours• Typical usage,10 minute for every hour:42mA=32 hours• EDGE completely idle: 5mA=9.5 days• Airplane mode idle:2mA=24 days#DV14 #BatteryOptimization @maydintr 8. Bulk Data TransferBulk data transfer for a 6MB song:• EDGE (90kbps):300mA*9.1 min = 45 mAh• 3G (300kbps):210mA*2.7 min = 9.5 mAh• WiFi (1Mbps):330mA*48 sec = 4.4 mAh#DV14 #BatteryOptimization @maydintr 9. • Research by Purdue University into energy consumptionby apps (March 2012)“Free apps like Free Chess and Angry Birds spend under25-35 percent of their energy on game play, but over 65-75percent on user tracking, uploading user information anddownloading ads.”#DV14 #BatteryOptimization @maydintr 10. Radio State Machine#DV14 #BatteryOptimization @maydintr 11. Bundled vs Unbundled Transfers• Suppose 3 data transfereach 3s in every 20s• Unbundled 1 datatransfer =3s+5s+12s=20s• 20s x 3 totally 60s• Bundled 9s+5s+12s =totally 26s#DV14 #BatteryOptimization @maydintr 12. Prefetch Data• Reduce download times• Minimize in-app latency• Dont use it aggressively• Prefetch data such that you will only need to initiateanother download every 2 to 5 minutes, and in the orderof 1 to 5 megabytes#DV14 #BatteryOptimization @maydintr 13. Batch Data• Create a pending transfer queue• Transfer from the queue when there is scheduledupdates#DV14 #BatteryOptimization @maydintr 14. Use Traffic Stats APITrafficStats.setThreadStatsTag(0xF00D);try {// Make network request using HttpClient.execute()} finally {TrafficStats.clearThreadStatsTag();}#DV14 #BatteryOptimization @maydintr 15. Polling?• Use GCM instead of polling• Use Inexact Repeating Alarms for pollings• Dont use _WAKEUP versions of alarm types• Implement exponential back-off pattern for pollings#DV14 #BatteryOptimization @maydintr 16. Cache Data• Cache as much as possible• Be careful about displaying stale dataMonitor Connectivity• Avoid connection attempts if there is no network• Modify download pattern based on the connectivity type#DV14 #BatteryOptimization @maydintr 17. Compress Data• Compress data before transferring• Especially textual dataimport;HttpGet request = new HttpGet(“http://url”);HttpResponse resp = new DefaultHttpClient().execute(request);HttpEntity entity = response.getEntity();InputStream compressed = entity.getContent();InputStream rawData = new GZIPInputStream(compressed);#DV14 #BatteryOptimization @maydintr 18. Compress DataDerived from Jeff Sharkey’s presentation#DV14 #BatteryOptimization @maydintr 19. Battery Monitoring• Monitor changes in charging state• Monitor Significant Changes in Battery Level• Maximize update rate when device is charging, minimizewhen discharging• Decrease update frequency when the battery is low#DV14 #BatteryOptimization @maydintr 20. Wakelock• Q: What is wakelock?• A: A wake lock is a mechanism to indicate that yourapplication needs to have the device stay on• Device battery life will be significantly affected by theuse of this API. Do notacquire PowerManager.WakeLocks unless you reallyneed them, use the minimum levels possible, and besure to release them as soon as possible.#DV14 #BatteryOptimization @maydintr 21. Wakelock• Use keepScreenOn instead of wakelock#DV14 #BatteryOptimization @maydintr 22. Derived from Jeff Sharkey’s presentation#DV14 #BatteryOptimization @maydintr 23. GPS• Use coarse network location if possible#DV14 #BatteryOptimization @maydintr 24. Trepn Profiler• Real-time overlay-mode allows developers to see theimpact of their actions on performance• Helps developers optimize code for CPU, power usageand network usage• Profiles the entire system or a single app• Displays accurate battery power readings with charts• Supports offline analysis of data• Can be started or stopped from a script for Automatedtests• Eclipse plug-in#DV14 #BatteryOptimization @maydintr 25. Demo#DV14 #BatteryOptimization @maydintr 26. As a Result#DV14 #BatteryOptimization @maydintr 27. Questions?• Twitter: @maydintr• Email:• Web: #BatteryOptimization @maydintr
Tips for Better Battery