Below code can be used to query the battery status. This uses a sticky intent concept.
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = registerReceiver(null, ifilter);
// Are we charging / charged?
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
Log.v(LOG_TAG,"Charging status is :"+status);
// How are we charging?
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
Log.v(LOG_TAG,"Charging Via :"+chargePlug);
Intent batteryLevelIntent = new Intent(BROADCAST_ACTION);
batteryLevelIntent.putExtra("chargingStatus",status);
batteryLevelIntent.putExtra("chargingVia",chargePlug);
batteryLevelIntent.putExtra("statusQueried",true);
LocalBroadcastManager.getInstance(AppUtils.getAppContext()).sendBroadcast(intent);
Same thing can be done inside an intent receive method. We need to add below to the AndroidManifest xml file
References:
No comments:
Post a Comment