Skip to content

Battery Optimization Secrets in Android Apps

Developing Android applications doesn’t simply include consideration of better Processes and Services, it also requires few other quality checks. One among them is considering battery usage.

It’s not a simple subject to avoid since it is also a major cause of retention of users. Simply imagine yourself downloading and installing a new app from the play store and you find your battery drained in the next couple of hours; then what, the next thing you will do is to uninstall the app, no matter what content the app provides you.

Here are few tips to improve your battery life in an android application

 

1. Try to reduce network call methods

Try to reduce network call methods in your code as far as possible. Instead, write down a snippet to Cache the data on first download and use it when the data is needed the next time.

Another best practice is to batch the network calls. Batching the network calls whenever possible helps prevent the device from waking up and sleeping down every often

 

2. Use wake locks only if needed

If you are using wake lock in your code, it is not considered a good idea most of the time. For reduced power consumption, always try to avoid wake lock. So what if you need to put your app awake on devices without draining users phone batteries? Well, that is not possible to keep the screen awake in no power.

But there is an alternate! You can use “ android:keepScreenOn="true" ” instead of WAKE_LOCK.

 

It is the same as using FLAG_KEEP_SCREEN_ON in code. You may use either. It’s upto you using them.

Here’s an example:

 

3. Use AlarmManager class very brainly

Don’t use frequent alarm calls. This wakes up the device in random times which also drain the battery backup at the mean time.

Better avoid your sync operation basing on clock time. Use ELAPSED_REALTIME alarm type if possible since scaling may not always do well if it is based on clock time.

 

4. Put Wifi connectivity and Mobile data connectivity their separate rules

Code the different rules to be optimized for each of them, so that your app may consume less power while using wifi connection compared to the power consumption by Mobile data.

 

5. Check all your background services

A background service may consume much amount of power if it becomes buggy at times. Since a service can restart itself even after it is killed externally, it is not a good idea to use them unless you really need it in your app.

 

6. GPS is a battery drainer

Don’t use GPS often in your app. But what to do if your app needs GPS to be turned ON to work? Well, you may try the tricks followed by apps like OLA cabs.

Follow this StackOverflow answer to read more
https://stackoverflow.com/questions/4721449/how-can-i-enable-or-disable-the-gps-programmatically-on-android/33555732#33555732

So how can I test if my app eats up a lot of energy?

Battery Historian

Use Battery Historian tool! The tool will display details of your phone’s battery usage in an HTML view.

It will inspect the battery stats and also show the statistics of all the events that were run after the device was plugged off the supply.

For more information on the topic, read out the Official Google documentation here
https://developer.android.com/studio/profile/battery-historian.html

This Post Has 0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top