1.获取Android设备的电池信息需要注册一个Action为Intent.ACTION_BATTERY_CHANGED的BroadCastReceiver。
2.在BroadcastReceiver的onReceive()事件,接收到的Intent.ACTION_BATTERY_CHANGED,包括下面的信息:
"present" (boolean) ...
"level" (int) …电池剩余容量
"scale" (int) …电池最大值,通常为100。
"icon-small" (int) …图标ID。
"voltage" (int) …电池的电压(伏特)
"temperature" (int) …电池的温度,0.1度单位。例如 表示197的时候,意思为19.7度。
"technology" (String) …电池类型,例如,Li-ion等等。
"plugged" (int) …充电方式:
BatteryManager.BATTERY_PLUGGED_AC:AC充电。
BatteryManager.BATTERY_PLUGGED_USB:USB充电。
"status" (int) …电池状态:
BatteryManager.BATTERY_STATUS_CHARGING:充电状态。
BatteryManager.BATTERY_STATUS_DISCHARGING:放电状态。
BatteryManager.BATTERY_STATUS_NOT_CHARGING:未充满。
BatteryManager.BATTERY_STATUS_FULL:充满电。
BatteryManager.BATTERY_STATUS_UNKNOWN:未知状态。
"health" (int) …健康状态:
BatteryManager.BATTERY_HEALTH_GOOD:状态良好。
BatteryManager.BATTERY_HEALTH_DEAD:电池没有电。
BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:电池电压过高。
BatteryManager.BATTERY_HEALTH_OVERHEAT:电池过热。
BatteryManager.BATTERY_HEALTH_UNKNOWN:未知状态。
public class BatteryActivity extends Activity{ private BatteryReceiver batteryReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.battary);
batteryReceiver = new BatteryReceiver(); IntentFilter batteryFilter = new IntentFilter(); batteryFilter.addAction(Intent.ACTION_BATTERY_CHANGED); registerReceiver(batteryReceiver, batteryFilter); } private class BatteryReceiver extends BroadcastReceiver{ @Override public void onReceive(Context arg0, Intent arg1) { int level = arg1.getIntExtra("level", 0); Log.i("Battery", "当前电池的剩余电量为:" + level + "%"); } } @Override protected void onDestroy() { unregisterReceiver(batteryReceiver); super.onDestroy(); } }