注册广播
一、生成广播
public class BatteryReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
int icon =intent.getIntExtra(BatteryManager.EXTRA_ICON_SMALL, 0);
content.setImageResource(icon);
}
}
}
//注册监听
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
BatteryReceiver mReceiver = new BatteryReceiver();
registerReceiver(mReceiver, filter);
//电池监听信息
1、字段
电池健康状态
BatteryManager.EXTRA_HEALTH
int BATTERY_HEALTH_COLD
int BATTERY_HEALTH_DEAD
int BATTERY_HEALTH_GOOD
int BATTERY_HEALTH_OVERHEAT
int BATTERY_HEALTH_OVER_VOLTAGE
int BATTERY_HEALTH_UNKNOWN
int BATTERY_HEALTH_UNSPECIFIED_FAILURE
电池当前状态图片
EXTRA_ICON_SMALL
电池充电方式