Battery Optimization for Android Apps
Reducing battery usage in an Android application
This article is all about how to develop an Android application considering the battery usage.
Battery Usage Reduction is also an important part of Android development as this optimization will ultimately lead to retain the user, as many times the user uninstalls the application because of the battery draining issue.
Tips for improving battery usage in an android application:
- Reduce network calls as much as you can: Cache your data and retrieve it from the cache when required next time.
- Avoid wake lock as much as possible: A wake lock is a mechanism to indicate that your application needs to have the device stay on.
- Use AlarmManager carefully: Wrong use of AlarmManager can easily drain the battery.
- Batch the network calls: You should batch the network calls if possible so that you can prevent the device from waking every second.
- A different logic for Mobile Data and Wifi: You should write different logic for mobile data and wifi as one logic may be optimized for mobile data and others may be optimized for wifi.
- Check all background processes: You should check all the background processes.
- Use GPS carefully: Do not use it frequently, use it only when actually required.
- Use WorkManager: As the official documentation says, WorkManager is an API that makes it easy to schedule deferrable, asynchronous tasks that are expected to run even if the app exits or the device restarts. The WorkManager API is a suitable and recommended replacement for all previous Android background scheduling APIs, including FirebaseJobDispatcher, GcmNetworkManager, and Job Scheduler. WorkManager incorporates the features of its predecessors in a modern, consistent API that works back to API level 14 while also being conscious of battery life.
- App Standby Buckets: In earlier versions of Android, Google introduced features like Doze and App Standby modes which saves users’ battery. Android Pie (Version 9 API level 28) introduced a new feature for better battery(power) management called App Standby Buckets. Each android application is now placed into one of the priority buckets based on the app’s usage patterns like how recently & how frequently the user has used the application. The android system then limits the app’s resources based on the bucket app is currently residing in. Learn more about it here.
Now, the next question is: Which tool should I use to check the battery usage of the Android app?
Battery Historian is a tool that displays information about your phone’s battery usage in HTML form.
It is a tool to inspect battery related information and events on an Android device running Android 5.0 Lollipop (API level 21) and later, while the device was not plugged in. It allows application developers to visualize the system and application-level events on a timeline with panning and zooming functionality, easily sees various aggregated statistics since the device was last fully charged, and select an application and inspect the metrics that impact battery specific to the chosen application. It also allows an A/B comparison of two bug-reports, highlighting differences in key battery-related metrics.
You will be able to get the following from the Battery Historian:
- CPU running time.
- Screen uptime.
- Mobile radio status.
- Data connection usage.
- Doze mode status.
- Charging status.
- Wifi running time.
- Change in signal strength.
- Detect the wake lock.
- and much more.
That's it for now.
Happy Learning :)
Show your love by sharing this blog with your fellow developers.