27

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.

jimmym715
  • 1,514
  • 1
  • 16
  • 25
Jeremy Edwards
  • 14,108
  • 15
  • 71
  • 98

4 Answers4

70

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.

plowman
  • 12,560
  • 7
  • 45
  • 48
  • 5
    This is a great example, but why you logging it on "error" level? – GetUsername Jun 29 '11 at 07: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 '11 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.

Jarett Millard
  • 5,275
  • 4
  • 38
  • 48
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) + "%";
    }
XXX
  • 8,662
  • 7
  • 42
  • 52
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
Has AlTaiar
  • 3,790
  • 1
  • 33
  • 36