Skip to content Skip to sidebar Skip to footer

Android Batterymanager Returning 0 For All Property-retrieval Calls

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_CURREN

Solution 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.

Solution 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.

privatevoidgetBatteryPercentage() {
    BroadcastReceiverbatteryLevelReceiver=newBroadcastReceiver() {
        publicvoidonReceive(Context context, Intent intent) {
            context.unregisterReceiver(this);
            intcurrentLevel= intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            intscale= intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            intlevel= -1;
            if (currentLevel >= 0 && scale > 0) {
                level = (currentLevel * 100) / scale;
            }
            Toast.makeText(MainActivity.this, level + "%", Toast.LENGTH_LONG).show();
        }
    };
    IntentFilterbatteryLevelFilter=newIntentFilter(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(batteryLevelReceiver, batteryLevelFilter);
}

Solution 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.

Post a Comment for "Android Batterymanager Returning 0 For All Property-retrieval Calls"