Reputation: 21
How to display the batterylevel of the phone in android as a textview ?
Upvotes: 1
Views: 665
Reputation: 3088
If you simply want to display the current battery level as a percentage, it's not necessary to register a receiver and so on. This will do it:
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = getActivity().registerReceiver(null, ifilter);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = level / (float)scale;
The battery level of my device is currently 94% - batteryPct has a value of 0.94.
This could then be displayed in a TextView like this:
textView.setText(Float.toString(batteryPct * 100) + "%"); // Displays '94.0%' using my example value
See: http://developer.android.com/training/monitoring-device-state/battery-monitoring.html
Upvotes: 1
Reputation: 3874
from link here link
receiver
public class PowerConnectionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
int chargePlug = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
}
}
AndroidManifest.xml add below
code changes
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.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;
// 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;
Upvotes: 0
Reputation: 1148
The best way is to use a broadcast receiver, as this will fire the onRecieve every time the state of the battery changes, so as the charge drops your values will always be accurate.
In your activity's onCreate you need to register a broadcast receiver for the battery state:
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_POWER_CONNECTED);
filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
batteryMonitor = new batteryChargeMonitor();
getActivity().registerReceiver(batteryMonitor, filter);
The batteryMonitor class looks like this:
private class batteryChargeMonitor extends BroadcastReceiver{
public void onReceive(Context context, Intent intent)
{
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
if(status > -1 && scale > -1 && level > -1)
{
float charge = (level/(float)scale) * 1oo;
Toast.makeText(context, "Battery Charge: " + charge + "%" Toast.LENGTH_LONG).show();
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING
|| status == BatteryManager.BATTERY_STATUS_FULL;
}
}
}
Upvotes: 0
Reputation: 2445
The tutorial is Here. In particular:
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = level / (float)scale;
Upvotes: 0
Reputation:
Before asking any question please do use Google .By The Way Click Here I hope it helps you .
Upvotes: 1