28

Is there a way to get battery information from the Android SDK? Such as battery life remaining and so on? I cannot find it through the docs.

4 Answers 4

71

Here is a quick example that will get you the amount of battery used, the battery voltage, and its temperature.

Paste the following code into an activity:

@Override
public void onCreate() {
    BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
        int scale = -1;
        int level = -1;
        int voltage = -1;
        int temp = -1;
        @Override
        public void onReceive(Context context, Intent intent) {
            level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
            voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
            Log.e("BatteryManager", "level is "+level+"/"+scale+", temp is "+temp+", voltage is "+voltage);
        }
    };
    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(batteryReceiver, filter);
}

On my phone, this has the following output every 10 seconds:

ERROR/BatteryManager(795): level is 40/100 temp is 320, voltage is 3848

So this means that the battery is 40% full, has a temperature of 32.0 degrees celsius, and has voltage of 3.848 Volts.

2
  • 5
    This is a great example, but why you logging it on "error" level? Jun 29, 2011 at 7:11
  • 3
    @GetUsername There's no particular reason for error-level logging other than personal preference. For logging like this that I remove before publishing my app, I like to log at the error level so it is easy to see the logging in the sea of logcat output.
    – plowman
    Jun 29, 2011 at 17:15
23

You can register an Intent receiver to receive the broadcast for ACTION_BATTERY_CHANGED: http://developer.android.com/reference/android/content/Intent.html#ACTION_BATTERY_CHANGED. The docs say that the broadcast is sticky, so you'll be able to grab it even after the moment the battery state change occurs.

1
  • 2
    Can you post some sample code for this. I'm not sure how to receive a broadcast intent. Dec 21, 2009 at 5:59
2
    public static String batteryLevel(Context context)
    {
        Intent intent  = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));   
        int    level   = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        int    scale   = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
        int    percent = (level*100)/scale;
        return String.valueOf(percent) + "%";
    }
0

I needed to have a monitor on the battery and check the Level and the status. I am developing on MonoForAndroid, so here is what I came up with. I am putting it here in case somebody have a similar requirement. (I have tested this and works nicely).

try
{
    var ifilter = new IntentFilter(Intent.ActionBatteryChanged);
    Intent batteryStatusIntent = Application.Context.RegisterReceiver(null, ifilter);
    var batteryChangedArgs = new AndroidBatteryStateEventArgs(batteryStatusIntent);
    _Battery.Level = batteryChangedArgs.Level;
    _Battery.Status = batteryChangedArgs.BatteryStatus;
}
catch (Exception exception)
{
    ExceptionHandler.HandleException(exception, "BatteryState.Update");
    throw new BatteryUpdateException();
}

namespace Leopard.Mobile.Hal.Battery
{
    public class AndroidBatteryStateEventArgs : EventArgs
    {
        public AndroidBatteryStateEventArgs(Intent intent)
        {
        Level = intent.GetIntExtra(BatteryManager.ExtraLevel, 0);
        Scale = intent.GetIntExtra(BatteryManager.ExtraScale, -1);
        var status = intent.GetIntExtra(BatteryManager.ExtraStatus, -1);
        BatteryStatus = GetBatteryStatus(status);
    }

    public int Level { get; set; }
    public int Scale { get; set; }
    public BatteryStatus BatteryStatus { get; set; }

    private BatteryStatus GetBatteryStatus(int status)
    {
        var result = BatteryStatus.Unknown;
        if (Enum.IsDefined(typeof(BatteryStatus), status))
        {
            result = (BatteryStatus)status;
        }
        return result;
    }
}
}


#region Internals
public class AndroidBattery
{
    public AndroidBattery(int level, BatteryStatus status)
    {
        Level = level;
        Status = status;
    }

    public int Level { get; set; }
    public BatteryStatus Status { get; set; }
}

public class BatteryUpdateException : Exception
{
} 
#endregion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.