Check Android Device is in Charging and Check Battery Status of the device
To check whether android device is in charging :
Using the following code we can check 3 major factors in android related the battery option,
1. Check whether a device is in charging.
2. Check whether device is in USB charging or AC charging.
3. Check Battery Status / charge level of the device.
IntentFilter batteryFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = registerReceiver(null, batteryFilter);
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
// check in which way we are charging
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED,-1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
Toast.makeText(getApplicationContext(),"Hey! your charging using USB.",Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(),"Hey! your charging using AC line.",Toast.LENGTH_LONG).show();
Hope, this would help in developing an application for battery monitoring and charging monitoring.