Battery STAMINA Mode

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!

More information

Comments 15

Sort by: Newest | Oldest | Most popular

    • By Joe Padre

      #2

      Hi Emanuel,
      Thanks, will see if we can get an internal engineer to look into this.
      Best regards,
      Joe from Developer World

  1. By Java Proxy

    #3

    My app is an alarm clock.
    Stamina mode is the cause the alarm to be aborted, and users are not aware of it.
    Is there an API to determine if Stamina mode is on/if my app is excluded/a way to make a shortcut for the user to exclude my app?

    This feature is responsible to a nice portion of my 1 star reviews – thank you Sony

  2. By Morten Jørgensen

    #4

    We agree that Stamina mode seems very efficient, and it seems to work quite well.

    In our application, it is crucial that network trafic is always enabled.
    Using the whitelist option, network trafic during Stamina mode still works like a charm.

    But, here comes the problem:
    If user is using Stamina mode, but is not aware of the whitelisting option, our app will fail to do its job – we can accept this..

    But it is hard to accept that it’s not possible to advice the user to either 1. whitelist the app, or 2. disable Stamina mode. This is not possible, since it does not seem posible to programatically detect if the device is using Stamina mode. Is there any official / unofficial work around, to improve user experience in this case?

    (See: http://stackoverflow.com/questions/18845479/how-to-programmatically-detect-sony-stamina-device-energy-profile-is-on )

  3. By Gennaro Manzo

    #5

    Stamina mode in Xperia is one amazing feature
    that need a little improvement.
    Theres basically a little problem
    native on Android O.S . since his first release 1.5 cupcake

    we may optimize (near) any the app on the device

    but if the “background data transfer” still activate
    also if we have disabled all the connections and app
    meanwhile it consume the battery life !

    Also if you set all the power menagement correctly as you wrote
    “background data transfer” is still working
    until you disable it in “Data Usage” *

    The best feature that will may put is
    insert that improvement in “stamina mode”

    on few words
    automatically “disable background data transfer”
    when we set “Stamina Mode” and/or “battery power save”

    i think we could save few hours of usage
    for to work in “cellphone mode” and don’t miss any call
    or have the possibility to call

    instead of have a syncronized smartphone
    but not simply can’t call nobody when we need

  4. By Mohammed Yaseen

    #6

    Hi Anna,

    Can new firmware 10.1.1.A.1.253 for Xperia Z will solve battery drain problem. Initially when I got this mobile, battery was awesome but from past many weeks my battery is getting drain very soon.. could you please help me out in fixing this issue… You can mail me the solution at yaseen.royalmech@gmail.com

    Thanks & Regards,
    Yaseen.

  5. By Richard Elia Sgarbossa

    #7

    Hello, my name is Richard and a couple of months I have Xperia ze are satisfied, very satisfied, this smartphone. I come from iPhone and I wanted to change the operating system because it is too limited a little, and here’s my disappointment … it says that Android is an open system and allows various tests and changes … I tried to edit it, and after I learn that is no longer possible to have an original system (example: I can not transfer from iPhone backup all’xperia because no longer original) … What a disappointment … with the iPhone I could do anything to then return to the original system I know you can not keep … null and void with this, but I wanted to express my regret at not being able to use more as if it were original.
    Thanks for any reply

    • By Anna Aleryd

      #8

      Hi Richard,

      I’m sorry to hear you’re disappointed. However, I’m not sure I understand your problem, can you explain a bit more?

      If you’ve unlocked your boot loader using our unlock boot loader service, you have probably voided your warranty (dependent on market specific terms and conditions), which is clearly stated at http://unlockbootloader.sonymobile.com/.

      However, if you have an unlocked Xperia™ Z, you can flash generic Sony software using our flash tool, if your phone model is on the list of supported phones.

      But again, please explain more and we will do our best to give you guidance.

      Best regards,
      Anna from Developer World

      • By dominic owusu

        #9

        Anna, hw do i deal with my Xperia sola that continues restarting after the xperia logo after boot? pls help me

  6. By Henrik Ng

    #11

    My Xperia Z display was crack after the phone heat up by using data traffic. What should i do?

    • By Anna Aleryd

      #12

      Hi Henrik,

      Sorry to hear that! You should go to the Xperia care web site where you will find the contact details for your local customer support. They should be able to help you out!

      Br,
      Anna from Developer World

    • By Henrik Ng

      #15

      Dean, you are right… I am waiting till i had change my S to Z… too slow…

1-15 of 15 comments.