27

有没有办法从 Android SDK 获取电池信息?比如剩余电池寿命等等?我无法通过文档找到它。

4

4 回答 4

70

这是一个简单的示例,可以让您了解电池的使用量、电池电压和温度。

将以下代码粘贴到活动中:

@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);
}

在我的手机上,每 10 秒有以下输出:

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

所以这意味着电池电量为 40%,温度为 32.0 摄氏度,电压为 3.848 伏。

于 2011-02-23T22:09:47.803 回答
23

您可以注册一个 Intent 接收器来接收 ACTION_BATTERY_CHANGED 的广播:http: //developer.android.com/reference/android/content/Intent.html#ACTION_BATTERY_CHANGED。文档说广播是粘性的,所以即使在电池状态发生变化的那一刻你也可以抓住它。

于 2009-11-26T22:22:35.920 回答
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) + "%";
    }
于 2014-07-15T15:24:11.677 回答
0

我需要在电池上安装一个监视器并检查电量和状态。我正在 MonoForAndroid 上开发,所以这就是我想出的。我把它放在这里以防有人有类似的要求。(我已经对此进行了测试并且效果很好)。

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
于 2014-01-06T05:39:32.480 回答