SDK 中的 Android 电池

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1805895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:38:33  来源:igfitidea点击:

Android Battery in SDK

androidpower-management

提问by Jeremy Edwards

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.

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

采纳答案by Jarett Millard

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.

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

回答by plowman

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:

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

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

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.

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

回答by XXX

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

回答by Has AlTaiar

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).

我需要在电池上安装一个监视器并检查电平和状态。我正在 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