在Android中获取电池电量和状态

时间:2010-07-20 15:32:48

标签: java android battery

如何获得电池电量和状态(插入,放电,充电等)?我研究了开发人员文档,然后找到了一个BatteryManager类。但它不包含任何方法,只包含常量。我怎么用呢?

11 个答案:

答案 0 :(得分:108)

Here是一个代码示例,解释了如何获取电池信息。

总结一下,动态设置ACTION_BATTERY_CHANGED意图的广播接收器,因为它不能通过清单中声明的​​组件接收,只能通过Context.registerReceiver()显式注册它。 / p>

public class Main extends Activity {
  private TextView batteryTxt;
  private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver(){
    @Override
    public void onReceive(Context ctxt, Intent intent) {
      int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
      batteryTxt.setText(String.valueOf(level) + "%");
    }
  };

  @Override
  public void onCreate(Bundle b) {
    super.onCreate(b);
    setContentView(R.layout.main);
    batteryTxt = (TextView) this.findViewById(R.id.batteryTxt);
    this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
  }
}

答案 1 :(得分:72)

从API 21开始,可以使用以下方法将当前电池电量水平作为一个百分比:

BatteryManager bm = (BatteryManager)getSystemService(BATTERY_SERVICE);
int batLevel = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);

答案 2 :(得分:40)

基于official android docs,您可以在Helper或Util类中使用此方法来获取当前的电池百分比:

public static int getBatteryPercentage(Context context) {

    IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    Intent batteryStatus = context.registerReceiver(null, iFilter);

    int level = batteryStatus != null ? batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) : -1;
    int scale = batteryStatus != null ? batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1) : -1;

    float batteryPct = level / (float) scale;

    return (int) (batteryPct * 100);
}

答案 3 :(得分:9)

您不必注册实际的BroadcastReceiver,因为Android的BatteryManager使用粘性意图:

IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = registerReceiver(null, ifilter);

int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

float batteryPct = level / (float)scale;

return (int)(batteryPct*100);

这是来自https://developer.android.com/training/monitoring-device-state/battery-monitoring.html的官方文档。

答案 4 :(得分:6)

您可以使用它来获得剩余收费百分比。

private void batteryLevel() {
        BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                context.unregisterReceiver(this);
                int rawlevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
                int level = -1;
                if (rawlevel >= 0 && scale > 0) {
                    level = (rawlevel * 100) / scale;
                }
                batterLevel.setText("Battery Level Remaining: " + level + "%");
            }
        };
        IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(batteryLevelReceiver, batteryLevelFilter);
    }

答案 5 :(得分:5)

尝试此功能无需许可或任何收款人

void getBattery_percentage()
{
      IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
      Intent batteryStatus = getApplicationContext().registerReceiver(null, ifilter);
      int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
      int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
      float batteryPct = level / (float)scale;
      float p = batteryPct * 100;

      Log.d("Battery percentage",String.valueOf(Math.round(p)));
  }

答案 6 :(得分:1)

经过一些更多的研究,我找到了BatteryManager.EXTRA_LEVEL。

答案 7 :(得分:1)

 ga('create', 'UA-77806616-1', 'auto',{'name': 'newTracker'});
 ga('newTracker.require', 'ec');

ga('newTracker.ec:addPromo', {
        'id': 'xyz1', // Promotion ID - Required Type: string
        'name': 'name2', // Promotion Name - Type: string
        'creative':'', // Creative ad used for promotion - Type: string
        'position':''  // Position of the creative ad - Type: string
 });

答案 8 :(得分:1)

要检查电池电量百分比,我们使用 BatteryManager ,以下方法将返回电池电量百分比。

来源Link

public static float getBatteryLevel(Context context, Intent intent) {
    Intent batteryStatus = context.registerReceiver(null,
            new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
    int batteryLevel = -1;
    int batteryScale = 1;
    if (batteryStatus != null) {
        batteryLevel = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, batteryLevel);
        batteryScale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, batteryScale);
    }
    return batteryLevel / (float) batteryScale * 100;
}

答案 9 :(得分:0)

官方文档中的一个错误,您必须使用double而不是float。因为0.53F * 100F = 52F

int level = batteryStatus != null ? 
batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1) : -1; int scale = 
batteryStatus != null ? batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1) : -1; 
double batteryPct = (double) level / (double) scale; int percent = (int) (batteryPct * 100D);

答案 10 :(得分:0)

其他答案未提及如何access battery status(是否跳棋)。

IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);

// Are we charging / charged?
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                     status == BatteryManager.BATTERY_STATUS_FULL;

// How are we charging?
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;