Reputation: 4286
In the broadcast receiver for battery, I have following 3 lines of code:
int health = intent.getIntExtra(BatteryManager.EXTRA_HEALTH, 0);
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, 0);
int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);
The values for the above three is coming as 2,2 and 4.
How can I decode these values and show something meaningful to the user like for health, can't I some how computer if battery health is good, bad or needs replacement.
Thanks in advance
Upvotes: 1
Views: 755
Reputation: 3399
Under onCreate method use
@Override
public void onCreate() {
BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
int scale = -1;
int level = -1;
int voltage = -1;
int temp = -1;
@Override
public void onReceive(Context context, Intent intent) {
level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
temp = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, -1);
voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, -1);
Log.e("BatteryManager", "level is "+level+"/"+scale+", temp is "+temp+", voltage is "+voltage);
}
};
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryReceiver, filter);
}
If level = 20/100 means battery is 20% remaining temp is 320 then temperature is 32 degree celcuis. and if voltage is 4000, then voltage is 4.000 volts
Upvotes: 2
Reputation: 9217
use this for battery
switch (health) {
case BatteryManager.BATTERY_HEALTH_DEAD:
break;
case BatteryManager.BATTERY_HEALTH_GOOD:
break;
case BatteryManager.BATTERY_HEALTH_COLD:
break;
case BatteryManager.BATTERY_HEALTH_OVERHEAT:
break;
case BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE:
break;
default:
break;
}
use for pluged
switch (plugged ) {
case BatteryManager.BATTERY_PLUGGED_AC:
break;
case BatteryManager.BATTERY_PLUGGED_USB:
break;
case BatteryManager.BATTERY_PLUGGED_WIRELESS:
default:
break;
}
use for voltage
switch (voltage) {
case BatteryManager.BATTERY_STATUS_CHARGING:
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
break;
case BatteryManager.BATTERY_STATUS_FULL:
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
break;
case BatteryManager.BATTERY_STATUS_UNKNOWN:
break;
default:
break;
}
Upvotes: 2