开发者

Android Battery in SDK

Is there a way to get battery informat开发者_开发百科ion from the Android SDK? Such as battery life remaining and so on? I cannot find it through the docs.


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.


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.


    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) + "%";
    }


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜