Java code for fetching Android battery specific details

56 Views Asked by At

What is the java code to the following values from Android device

  1. the total number charge cycle till date
  2. the max charge level reached in last charge cycle

Tried to fetch total charge cycle using batteryStatus.getIntExtra(BatteryManager.EXTRA_CYCLE_COUNT,-1) but it returns 0

2

There are 2 best solutions below

0
snachmsm On

have you tried EXTRA_CYCLE_COUNT on device with Android API 34 at least (so Android 14)? this param was just introduced, won't work on older devices

personally I bet it won't work reliable on all devices with Android 14 (especially those which get 14 as update, not from factory), manufacturers should prepare their hardware for such feature

0
Amer Al  Munajjed On

try this code :

int level = battery.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = battery.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = level / (float)scale;
BroadcastReceiver mybatteryReceiver= new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            int rawlevel = intent.getIntExtra("level", -1);
            int scale = intent.getIntExtra("scale", -1);
            int level = -1;
            if (rawlevel >= 0 && scale > 0) {
                level = (rawlevel * 100) / scale;
            }
            Log.d("TEMP","Battery Level in % is:: " + level + "%");
        }
    };