5 coding tips to make your app power-efficient (and optimised for Sony’s STAMINA mode)
The battery life of smartphones and tablets is extremely important for many users, and in order to provide end users with good battery performance, device manufacturers have a joint responsibility together with all of you app developers. The power-save feature STAMINA mode, available on our latest Xperia™ devices including Xperia Z, is a clever way for us to improve the battery life for our users. And as an app developer, there are several things you can do to make your app more power efficient. Read on for five of our best tips, that will also ensure your app runs well with STAMINA mode turned on.
Battery STAMINA Mode is a feature for users that want to increase their standby time. It is turned off by default, but users can turn it on in the Settings to make their battery last longer. Battery STAMINA Mode increases the standby time by more than four times by reducing the scheduled background activities when the phone is in standby mode. This is done in the Android™ AlarmManager class, which won’t wake up the system on most alarms, and with a firewall that prevents data traffic through both mobile networks and Wi-Fi®.
Since we launched the Xperia™ Z, we have seen some questions in the Android community around how this affects apps, so we wanted to give you developers some advice around this.
The main thing to think about when making your app less power consuming is to only perform actions that you really need, and when you really need to. Below are five hands-on coding tips that will make your app more power efficient and well-behaved, while at the same time make sure it’s optimised for Battery STAMINA Mode. Even though these tips will make your app work well with Battery STAMINA Mode, they are also general tips for how to use Android alarms in a clever, energy efficient way. This means that when you implement these methods, you will make sure that your app saves power and runs well on any Android device.
1. Don’t use wake locks
Wake locks are in general quite bad for power consumption, as they keep the entire Android system awake. If wake locks are used, make sure that they are timed wake locks so that the taken wake lock does not exist for a too long time. Time your wake lock so that it is released after your task is complete. You can see it used in an example at Java Monday.
In addition, you should only use wake locks when it is absolutely necessary, for example to keep the screen on when reading a book or watching a movie. When you use a wake lock, make sure to choose the lowest possible level. For example, use PARTIAL_WAKE_LOCK rather than FULL_WAKE_LOCK.
2. Use wakeup alarms as little as possible
To use an alarm can be one way to keep updated on a certain interval, but instead of simply using alarms, consider if you can use intents instead. There are number of different events to listen for, like the network intents and charging events described below, which in many cases will remove the need of alarms. As it is impossible to get around STAMINA without being white listed, the right intent will help you perform your task when possible. And remember, every alarm that goes off has an impact on power consumption!
3. Use network intents to perform tasks while data is allowed
When your device connects to a network, an intent is sent. Many apps don’t check for these intents, but to make your application work properly, you can listen to network change intents that will tell you when communication is possible. To check for network connectivity you can, for example, use the following clause:
if (intent.getAction().equals(android.net.ConnectivityManager.CONNECTIVITY_ACTION)){
NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
//perform your action when connected to a network
}
4. Save certain tasks for when the device is charging
Battery STAMINA Mode will automatically be disabled when a charger is connected. If possible, consider listening for charging events in your application, and trigger synchronisation and update activities when a charging event is detected. This will also lead to a general save of battery power, as your application will perform its tasks while the device is plugged in.
5. Perform tasks when your application is actually being used
Instead of waking up automatically using alarms and intents, consider if it is enough to perform tasks once a user enters or starts the application.
***
In addition to these hands-on tips, we strongly advise you to always follow the Android activities and services lifecycle, to ensure that your application always functions in an optimal way. You can read more about the activity lifecycle on Android developer.
Now, if you follow this simple advice, you will ensure that your app works in a power efficient way, and that it always behaves as you intended, even if the users turn on Battery STAMINA Mode on their Xperia™ devices. If you have any questions, feel free to drop us a line below!
No comments: