Created
June 23, 2015 02:35
-
-
Save tunjos/b61734a0b03393507548 to your computer and use it in GitHub Desktop.
Checks if device is charging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private boolean checkForPower() { | |
// It is very easy to subscribe to changes to the battery state, but you can get the current | |
// state by simply passing null in as your receiver. Nifty, isn't that? | |
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); | |
Intent batteryStatus = this.registerReceiver(null, filter); | |
// There are currently three ways a device can be plugged in. We should check them all. | |
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); | |
boolean usbCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_USB); | |
boolean acCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_AC); | |
boolean wirelessCharge = false; | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { | |
wirelessCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_WIRELESS); | |
} | |
return (usbCharge || acCharge || wirelessCharge); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment