Android BatteryManager returning 0 for all property-retrieval calls

可紊 提交于 2019-12-10 16:47:28

问题


I'm having trouble trying to access most of the stats from my Android device's battery such as BATTERY_PROPERTY_CAPACITY, BATTERY_PROPERTY_CHARGE_COUNTER or BATTERY_PROPERTY_CURRENT_AVERAGE. These properties are all clearly documented here: http://developer.android.com/reference/android/os/BatteryManager.html

I have the following permission declared on my manifest:

<uses-permission android:name="android.permission.BATTERY_STATS" />

I have a non-null BatteryManager instance:

mBatteryManager = (BatteryManager) getSystemService(BATTERY_SERVICE);

And whenever I try to retrieve these stats:

double remainingCapacity = mBatteryManager.getIntProperty(
            BatteryManager.BATTERY_PROPERTY_CAPACITY);
double batteryCapacityMicroAh = mBatteryManager.getIntProperty(
            BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER);
double averageCurrentMicroA = mBatteryManager.getIntProperty(
            BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE);

The results are all 0 for all of those. Always, without fail, 0. I've tried this on an emulator, an actual device, and everything I've thought of has failed to modify my results.

I'd greatly appreciate any help. Ultimately, what I'm trying to do is calculate the time remaining for a device to reach its full charge (while charging). Thanks in advance.


回答1:


Not sure if this is any help for you now, but only phones that have a battery fuel gauge (https://source.android.com/devices/tech/power/device.html) can report back some of the values you are looking for. AFAIK, only the Nexus 6, 9, and 10 gives all three values you are looking for, with Nexus 5 giving only BATTERY_PROPERTY_CAPACITY.




回答2:


You need to register for Action_Battery_Changed broadcast . This will get invoked whenever this event occurs. Since this is a sticky Intent, it keeps on broadcasting once registered. The method below would get the current level of battery.

private void getBatteryPercentage() {
    BroadcastReceiver batteryLevelReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            context.unregisterReceiver(this);
            int currentLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            int level = -1;
            if (currentLevel >= 0 && scale > 0) {
                level = (currentLevel * 100) / scale;
            }
            Toast.makeText(MainActivity.this, level + "%", Toast.LENGTH_LONG).show();
        }
    };
    IntentFilter batteryLevelFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}



回答3:


Value of BatteryManager.EXTRA_LEVEL is current battery capacity. I guess you are looking for the total capacity(full), you need to read /sys/class/power_supply/battery/charge_full_design or charge_full, different manufacturers may have different path.



来源:https://stackoverflow.com/questions/32280582/android-batterymanager-returning-0-for-all-property-retrieval-calls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!