Created
December 21, 2016 13:26
-
-
Save talhahasanzia/4b7d3aedc3a117e2d55dd8291178d6b3 to your computer and use it in GitHub Desktop.
Track Battery Status in Android
This file contains hidden or 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
// 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); | |
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1); | |
boolean acCharge = (chargePlug == BatteryManager.BATTERY_PLUGGED_AC); | |
if (acCharge) { | |
Log.v(LOG_TAG,“The phone is charging!”); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment