mContext.registerReceiver(mBatteryChangedReceiver,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
private final BroadcastReceiver mBatteryChangedReceiver= new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
//int status=intent.getIntExtra(BatteryManager.EXTRA_STATUS,BatteryManager.BATTERY_STATUS_UNKNOWN);
BatteryManager batteryManager = context.getSystemService(BatteryManager.class);
int level = batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY); //电量
String batteryStatus = com.android.settingslib.Utils.getBatteryStatus(context, intent); //充电状态
if(batteryStatus.equals(context.getResources().getString(R.string.battery_info_status_charging_fast))){ //快充
//todo
}
}
};
- frameworks/base/packages/SettingsLib/src/com/android/settingslib/Utils.java
public static String getBatteryStatus(Context context, Intent batteryChangedIntent) {
final int status = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_STATUS,
BatteryManager.BATTERY_STATUS_UNKNOWN);
final Resources res = context.getResources();
String statusString = res.getString(R.string.battery_info_status_unknown);
final BatteryStatus batteryStatus = new BatteryStatus(batteryChangedIntent);
if (batteryStatus.isCharged()) {
statusString = res.getString(R.string.battery_info_status_full);
} else {
if (status == BatteryManager.BATTERY_STATUS_CHARGING) {
switch (batteryStatus.getChargingSpeed(context)) {
case BatteryStatus.CHARGING_FAST:
statusString = res.getString(R.string.battery_info_status_charging_fast);
break;
case BatteryStatus.CHARGING_SLOWLY:
statusString = res.getString(R.string.battery_info_status_charging_slow);
break;
default:
statusString = res.getString(R.string.battery_info_status_charging);
break;
}
} else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) {
statusString = res.getString(R.string.battery_info_status_discharging);
} else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) {
statusString = res.getString(R.string.battery_info_status_not_charging);
}
}
return statusString;
}
- frameworks/base/packages/SettingsLib/src/com/android/settingslib/fuelgauge/BatteryStatus.java
public boolean isCharged() {
return status == BATTERY_STATUS_FULL || level >= 100;
}
public final int getChargingSpeed(Context context) {
final int slowThreshold = context.getResources().getInteger(
R.integer.config_chargingSlowlyThreshold);
final int fastThreshold = context.getResources().getInteger(
R.integer.config_chargingFastThreshold);
return maxChargingWattage <= 0 ? CHARGING_UNKNOWN :
maxChargingWattage < slowThreshold ? CHARGING_SLOWLY :
maxChargingWattage > fastThreshold ? CHARGING_FAST :
CHARGING_REGULAR;
}
- frameworks/base/packages/SettingsLib/res/values-zh-rCN/string.xml
<string name="battery_info_status_unknown" msgid="268625384868401114">"未知"</string>
<string name="battery_info_status_charging" msgid="4279958015430387405">"正在充电"</string>
<string name="battery_info_status_charging_fast" msgid="8027559755902954885">"正在快速充电"</string>
<string name="battery_info_status_charging_slow" msgid="3190803837168962319">"正在慢速充电"</string>
<string name="battery_info_status_discharging" msgid="6962689305413556485">"未在充电"</string>
<string name="battery_info_status_not_charging" msgid="8330015078868707899">"已插入电源,但是现在无法充电"</string>
<string name="battery_info_status_full" msgid="4443168946046847468">"电量充足"</string>