android 电池信息 BatteryManager
引用:http://blog.csdn.net/maqiutian/article/details/7246949
Android中电池信息(Battery information)的取得
这里介绍电池信息的取得.
Android.content.BroadcastReceiver类
Android.os.BatteryManager类
● 电池信息的取得,调用registerReceiver()方法。
第1个参数,设置BroadcastReceiver实例
第2个参数,设置追加了Intent.ACTION_BATTERY_CHANGED处理的IntentFilter实例。
● 在BroadcastReceiver的onReceive()事件,接收到的Intent.ACTION_BATTERY_CHANGED,包括下面的信息。
“status”(int类型)…状态,定义值是BatteryManager.BATTERY_STATUS_XXX。
“health”(int类型)…健康,定义值是BatteryManager.BATTERY_HEALTH_XXX。
“present”(boolean类型)
“level”(int类型)…电池剩余容量
“scale”(int类型)…电池最大值。通常为100。
“icon-small”(int类型)…图标ID。
“plugged”(int类型)…连接的电源插座,定义值是BatteryManager.BATTERY_PLUGGED_XXX。
“voltage”(int类型)…mV。
“temperature”(int类型)…温度,0.1度单位。例如 表示197的时候,意思为19.7度。
“technology”(String类型)…电池类型,例如,Li-ion等等。
- package dev.demo.batterytest;
- import Android.app.Activity;
- import Android.content.BroadcastReceiver;
- import Android.content.Context;
- import Android.content.Intent;
- import Android.content.IntentFilter;
- import Android.os.BatteryManager;
- import Android.os.Bundle;
- import Android.util.Log;
- public class BatteryTestActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- @Override
- protected void onResume() {
- super.onResume();
- IntentFilter filter = new IntentFilter();
- filter.addAction(Intent.ACTION_BATTERY_CHANGED);
- registerReceiver(mBroadcastReceiver, filter);
- }
- @Override
- protected void onPause() {
- super.onPause();
- unregisterReceiver(mBroadcastReceiver);
- }
- private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- String action = intent.getAction();
- if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
- int status = intent.getIntExtra("status", 0);
- int health = intent.getIntExtra("health", 0);
- boolean present = intent.getBooleanExtra("present", false);
- int level = intent.getIntExtra("level", 0);
- int scale = intent.getIntExtra("scale", 0);
- int icon_small = intent.getIntExtra("icon-small", 0);
- int plugged = intent.getIntExtra("plugged", 0);
- int voltage = intent.getIntExtra("voltage", 0);
- int temperature = intent.getIntExtra("temperature", 0);
- String technology = intent.getStringExtra("technology");
- String statusString = "";
- switch (status) {
- case BatteryManager.BATTERY_STATUS_UNKNOWN:
- statusString = "unknown";
- break;
- case BatteryManager.BATTERY_STATUS_CHARGING:
- statusString = "charging";
- break;
- case BatteryManager.BATTERY_STATUS_DISCHARGING:
- statusString = "discharging";
- break;
- case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
- statusString = "not charging";
- break;
- case BatteryManager.BATTERY_STATUS_FULL:
- statusString = "full";
- break;
- }
- String healthString = "";
- switch (health) {
- case BatteryManager.BATTERY_HEALTH_UNKNOWN:
- healthString = "unknown";
- break;
- case BatteryManager.BATTERY_HEALTH_GOOD:
- healthString = "good";
- break;
- case BatteryManager.BATTERY_HEALTH_OVERHEAT:
- healthString = "overheat";
- break;
- case BatteryManager.BATTERY_HEALTH_DEAD:
- healthString = "dead";
- break;
- case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
- healthString = "voltage";
- break;
- case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE:
- healthString = "unspecified failure";
- break;
- }
- String acString = "";
- switch (plugged) {
- case BatteryManager.BATTERY_PLUGGED_AC:
- acString = "plugged ac";
- break;
- case BatteryManager.BATTERY_PLUGGED_USB:
- acString = "plugged usb";
- break;
- }
- Log.v("status", statusString);
- Log.v("health", healthString);
- Log.v("present", String.valueOf(present));
- Log.v("level", String.valueOf(level));
- Log.v("scale", String.valueOf(scale));
- Log.v("icon_small", String.valueOf(icon_small));
- Log.v("plugged", acString);
- Log.v("voltage", String.valueOf(voltage));
- Log.v("temperature", String.valueOf(temperature));
- Log.v("technology", technology);
- }
- }
- };