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

发布时间:2023年12月28日
    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>
文章来源:https://blog.csdn.net/Sqq_yj/article/details/135267908
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。