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; } }
“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等等。更详细的我们不深究了,继续回归我们上面的问题
<action android:name="android.intent.action.ACTION_BATTERY_LOW"/> <action android:name="android.intent.action.ACTION_BATTERY_OKAY"/>
public abstract void sendStickyBroadcast (Intent intent) Since: API Level 1 Perform a sendBroadcast(Intent) that is "sticky," meaning the Intent you are sending stays around after the broadcast is complete, so that others can quickly retrieve that data through the return value of registerReceiver(BroadcastReceiver, IntentFilter). In all other ways, this behaves the same as sendBroadcast(Intent). You must hold the BROADCAST_STICKY permission in order to use this API. If you do not hold that permission, SecurityException will be thrown. Parameters intent The Intent to broadcast;
all receivers matching this Intent will receive the broadcast, and the Intent will be held to be re-broadcast to future receivers.
<uses-permission android:name="android.permission.BROADCAST_STICKY"/>
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent batteryStatus = context.registerReceiver(null, ifilter);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1); float batteryPct = level / (float)scale;
原文地址:http://blog.csdn.net/jaysong2012/article/details/45601001