• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Battery Manager in Android

Java:
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        final Intent batteryStatus = registerReceiver(null, ifilter);

        // Are we charging / charged?
        int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
        boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                status == BatteryManager.BATTERY_STATUS_FULL;

        // How are we charging?
        int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
        boolean usbCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_USB);
        boolean acCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_AC);


When the device is connected to USB, it still shows usbCharge = false, while at that time acCharge is shown as true.
Moreover, while assigning the value of chargePlug here, it is either assigning 0 or 1, no other value. Why is it so?
 
Last edited:
Back
Top Bottom