Android获取电池充电状态是否为快充

    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
    "未知"
    "正在充电"
    "正在快速充电"
    "正在慢速充电"
    "未在充电"
    "已插入电源,但是现在无法充电"
    "电量充足"

你可能感兴趣的:(framework,android,android,framework,systemui)