Check Android Device is in Charging and Check Battery Status of the device

by Sasikumar 2014-02-24 10:04:50

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;

if(usbCharge){
Toast.makeText(getApplicationContext(),"Hey! your charging using USB.",Toast.LENGTH_LONG).show();
}
else{
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.
540
like
0
dislike
0
mail
flag

You must LOGIN to add comments