在Android中获取电池电量和状态
如何获得电池电量和状态(插入,放电,充电等)? 我研究了开发者文档,并且find了一个BatteryManager类。 但是它不包含任何方法,只是常量。 我怎样才能使用它?
这里是一个代码示例,解释如何获取电池信息: http : //www.tutorialforandroid.com/2009/01/getting-battery-information-on-android.html
综上所述, ACTION_BATTERY_CHANGED
意图的广播接收器是dynamicbuild立的,因为它不能通过在manifest中声明的组件接收,只能通过Context.registerReceiver()
明确注册。
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)); } }
由于API 21可以使用以下方式获取当前电池电量百分比:
BatteryManager bm = (BatteryManager)getSystemService(BATTERY_SERVICE); int batLevel = bm.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
基于官方的android文档 ,你可以在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); }
你可以用这个来获得剩余的百分比。
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); }
经过一些更多的研究,我发现了BatteryManager.EXTRA_LEVEL。
您不必注册一个实际的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的官方文档。;
private void batteryLevel() { BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() { public void onReceive(Context context, Intent intent) { context.unregisterReceiver(this); int rawlevel = intent.getIntExtra("level", -1); int scale = intent.getIntExtra("scale", -1); int level = -1; if (rawlevel >= 0 && scale > 0) { level = (rawlevel * 100) / scale; } mBtn.setText("Battery Level Remaining: " + level + "%"); } }; IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); registerReceiver(batteryLevelReceiver, batteryLevelFilter); }