您当前的位置:首页 > 计算机 > 编程开发 > 安卓(android)开发

获取手机电池信息及电池剩余使用时间问题说明

时间:02-13来源:作者:点击数:

1.获取电池信息

通过电池状态改变广播

private BroadcastReceiver mBatInfoReceiver=new BroadcastReceiver(){
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
                BatteryInfoEvent batteryInfoEvent=new BatteryInfoEvent();
                //当前电量
                int intLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
                int intScale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
                batteryInfoEvent.setBatteryPercent(intLevel * 100 / intScale);
                //电池温度
                int temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
                float batteryTemperature = temperature / 10;
                batteryInfoEvent.setBatteryTemperature(batteryTemperature);

                //充电状态
                int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS,-1);
                String desc="";
                switch (status){
                    // 正充电
                    case BatteryManager.BATTERY_STATUS_CHARGING:
                        desc="正在充电";
                        break;
                    // 已经充满
                    case BatteryManager.BATTERY_STATUS_FULL:
                        desc="已充满";
                        break;
                    // 正断开充电
                    case BatteryManager.BATTERY_STATUS_DISCHARGING:
                        desc="未充电";
                        break;
                    // 没有充电
                    case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
                        desc="未充电";
                        break;
                    // 未知状态
                    case BatteryManager.BATTERY_STATUS_UNKNOWN:
                        desc="未知状态";
                        break;
                    case -1:{
                        desc="获取充电状态失败";
                    }break;
                }
                batteryInfoEvent.setChargeStatus(status);

                //电池电压
                int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
                float batteryVoltage = voltage / 1000f;
                BigDecimal bd = new BigDecimal(batteryVoltage);
                double value = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();//保留两位小数
                batteryInfoEvent.setBatteryVoltage(value);

                //电池健康状态
                int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, -1);
                String batteryHealth = null;
                switch(health){
                    case BatteryManager.BATTERY_HEALTH_DEAD: batteryHealth = "没电";break;
                    case BatteryManager.BATTERY_HEALTH_GOOD: batteryHealth = "健康";break;
                    case BatteryManager.BATTERY_HEALTH_OVERHEAT: batteryHealth = "过热";break;
                    case BatteryManager.BATTERY_HEALTH_COLD: batteryHealth = "过冷";break;
                    case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE: batteryHealth = "过电压";break;
                    case BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE: batteryHealth = "未知错误";break;
                    case BatteryManager.BATTERY_HEALTH_UNKNOWN: batteryHealth = "未知";break;
                }
                batteryInfoEvent.setHealthStatus(health);
                batteryInfoEvent.setHealthStatusDesc(batteryHealth);

                //电池类型
                String technology = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY);
                batteryInfoEvent.setTechnology(technology);

                //连接电源类型
                int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
                String pluggedDesc="";
                switch (plugged){
                    case BatteryManager.BATTERY_PLUGGED_AC:{
                        pluggedDesc="充电器";
                    }break;
                    case BatteryManager.BATTERY_PLUGGED_USB:{
                        pluggedDesc="USB";
                    }break;
                    case BatteryManager.BATTERY_PLUGGED_WIRELESS:{
                        pluggedDesc="无线充电";
                    }break;
                    default:{
                        pluggedDesc="未充电";
                    }
                }
                batteryInfoEvent.setPlugged(plugged);
                batteryInfoEvent.setPluggedDesc(pluggedDesc);
            }
        }
    };

记得广播的注册与注销

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        registerReceiver(mBatInfoReceiver,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));//注册电池信息变化广播
    }


@Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBatInfoReceiver);//注销电池信息变化广播
      
    }

这样可以拿到系统能提供的电池相关的信息

电池充电状态以及电池瞬时电流之类的也可以通过BatteryManager来获取

BatteryManager batteryManager = (BatteryManager) context.getSystemService(android.content.Context.BATTERY_SERVICE);

        // 剩余电量百分比 BATTERY_PROPERTY_CAPACITY: 51
        int batteryCapacity =
                batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
        // 当前电池剩余容量(mAH) BATTERY_PROPERTY_CHARGE_COUNTER: 2192080
        int batteryChargeCounter =
                batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER);
        // 剩余能量(nWH) BATTERY_PROPERTY_ENERGY_COUNTER: -2147483648
        long batteryEnergyCounter =
                batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_ENERGY_COUNTER);
        // 瞬时电流(mA)  BATTERY_PROPERTY_CURRENT_NOW: 632324(负表示放电 正表示充电)
        int batteryCurrentNow =
                batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW);
        // 平均电流(mA)  BATTERY_PROPERTY_CURRENT_AVERAGE: -2147483648 (负表示放电 正表示充电)
        int batteryCurrentAverage =
                batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE);
        // 状态(获取电池充电状态需要api>=26)小于26的将获取不到
        int status =
                batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_STATUS);

2.电池电量剩余使用时间

这个在网上没有现成的实现方案,像目前市场上的应用 电池医生等等 计算电池剩余使用时间 电池充电充满时间 电池健康度(根据电池容量的损耗来计算) 很多都是假数据。许多厂商会有相应的显示,但是哪些数据是不开放给开发者的,都是系统级别的权限,常规的开发者根本无法获取到。所以,要相对比较准确的计算以上数据,其实是需要应用对电池进行长时间的监测,根据电池使用状态较稳定(即:假设我们要计算电池剩余使用时间,则手机处于一个稳定的使用状态(放电状态稳定)情况下,根据电池每下降1%所用的时间来反推电池剩余电量的使用时间),充电剩余时间同理。

方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门