Android examples for android.os:Battery
Check if Battery is Charging and handle Exception
import android.app.Application; import android.content.Intent; import android.content.IntentFilter; import android.os.BatteryManager; public class Main { public static boolean isCharging() { try {/* ww w.j av a2 s . c o m*/ IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent batteryStatus = getApplicationUsingReflection().registerReceiver( null, ifilter); int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1); boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL; if (isCharging) { return true; } else { return false; } } catch (Exception e) { return false; } } public static Application getApplicationUsingReflection() throws Exception { return (Application) Class.forName("android.app.ActivityThread") .getMethod("currentApplication").invoke(null, (Object[]) null); } }