Skip to content

Instantly share code, notes, and snippets.

@talhahasanzia
Created December 21, 2016 13:26
Show Gist options
  • Save talhahasanzia/4b7d3aedc3a117e2d55dd8291178d6b3 to your computer and use it in GitHub Desktop.
Save talhahasanzia/4b7d3aedc3a117e2d55dd8291178d6b3 to your computer and use it in GitHub Desktop.
Track Battery Status in Android
// 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