2

我可以用 读取瞬时电流的值,_CURRENT_NOW并用 获得剩余电池容量的百分比_CAPACITY。问题是我无法访问mAh该物业的剩余电池容量_CHARGE_COUNTER(也不_ENERGY_COUNTER)。是由于硬件原因还是在无根手机上不可能?我不明白为什么下面的代码不起作用。

        TextView current = (TextView) findViewById(R.id.current);
    TextView chargeCounter = (TextView) findViewById(R.id.chargeCounter);
    TextView batteryLevel = (TextView) findViewById(R.id.batteryLevel);
    TextView energyCounter = (TextView) findViewById(R.id.energyCounter);

    BatteryManager batteryManager = (BatteryManager) getSystemService(BATTERY_SERVICE);

    long cu =  batteryManager.getLongProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW);
    long q = batteryManager.getLongProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER);
    long level = batteryManager.getLongProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
    long energy = batteryManager.getLongProperty(BatteryManager.BATTERY_PROPERTY_ENERGY_COUNTER);

    current.setText("Current : " + String.valueOf(cu) + " mA");
    chargeCounter.setText("Remaining capacity : " + String.valueOf(q) + " uAh");
    batteryLevel.setText("Battery status : " + String.valueOf(level) + " %");
    energyCounter.setText("Battery status : " + String.valueOf(energy) + " %");

对于每个不起作用的属性,我都有这个巨大的数字:9223372036854775808

欲了解更多信息:https ://source.android.com/devices/tech/power/device

谢谢!

4

1 回答 1

2

值为 Long.MIN_VALUE,因为你的手机不支持,你可以在https://developer.android.google.cn/reference/android/os/BatteryManager.html看到

于 2017-10-25T09:05:08.253 回答