Reputation: 1012
I'm creating a battery monitor application, and I'm trying to display the current battery percentage. I have a broadcastreceiver, that is listening for...
<action android:name="android.intent.action.BATTERY_CHANGED" />
In the receiver I have the following code.
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
currentBatteryLevel = level / (float) scale;
Toast.makeText(context, "Current battery level: " + Float.toString(currentBatteryLevel) + "%", Toast.LENGTH_SHORT).show();
For some reason, the result is always Current battery level: 1.0%
...Where am I going wrong?
Edit:
Reciever is registered in the manifest as...
<receiver android:name=".PowerMonitorReceiver">
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
<action android:name="android.intent.action.BATTERY_LOW" />
<action android:name="android.intent.action.BATTERY_OKAY" />
<action android:name="android.intent.action.BATTERY_CHANGED" />
</intent-filter>
</receiver>
and my receiver is...
package com.garciaericn.t2d;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.BatteryManager;
import android.util.Log;
import android.widget.Toast;
public class PowerMonitorReceiver extends BroadcastReceiver {
private static final String TAG = "PowerMonitorReceiver.TAG";
private float currentBatteryLevel;
@Override
public void onReceive(Context context, Intent intent) {
// Obtain action from intent to check which broadcast is being received
String action = intent.getAction();
// Perform action according to type
switch (action) {
case (Intent.ACTION_BATTERY_CHANGED): {
Log.i(TAG, "Battery has changed");
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
currentBatteryLevel = (level * 100) / (float) scale;
Toast.makeText(context, "Current battery level: " + Float.toString(currentBatteryLevel) + "%" + " level: " + level + " scale: " + scale, Toast.LENGTH_LONG).show();
break;
}
case (Intent.ACTION_POWER_CONNECTED): {
Log.i(TAG, "Power connected");
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;
String chargingType = null;
if (usbCharge) {
chargingType = "USB";
} else if (acCharge) {
chargingType = "AC Power";
}
if (isCharging && chargingType != null) {
Toast.makeText(context, "Device is charging via: " + chargingType, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Device is charging.", Toast.LENGTH_SHORT).show();
}
break;
}
case (Intent.ACTION_POWER_DISCONNECTED): {
Log.i(TAG, "Power disconnected");
Toast.makeText(context, "Power Disconnected", Toast.LENGTH_SHORT).show();
break;
}
case (Intent.ACTION_BATTERY_LOW): {
Log.i(TAG, "Battery low");
Toast.makeText(context, "Battery low", Toast.LENGTH_SHORT).show();
break;
}
case (Intent.ACTION_BATTERY_OKAY): {
Log.i(TAG, "Battery Okay");
Toast.makeText(context, "Battery Okay", Toast.LENGTH_SHORT).show();
break;
}
}
}
}
Upvotes: 2
Views: 10481
Reputation: 511
boolean acCharge = false;
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = registerReceiver(null, ifilter);
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
if(isCharging){
chargePercentage =
batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
}
The chargePercentage variable will give you the current charge percentage.
Upvotes: 0
Reputation: 3918
I think it's pretty late but the solution is quite simple. According to this link, you will not receive updates for BATTERY_CHANGED if you have registered the broadcast receiver in xml since it's a sticky broadcast.
You need to register Broadcastreceivers in your java program as below.
IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);
int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
float batteryPct = (level * 100) / (float)scale;
NOTE: Even though you pass "null" as the receiver you can still receive all the required battery data. You can also pass a Receiver class instead of "null" for continuous monitoring but beware this will drain your device battery as these broadcasts are thrown very often.
Upvotes: 6
Reputation: 20211
Since you are listening for multiple broadcasts, you need to determine which type of broadcast it is.
String action = intent.getAction();
if(action.equals(Intent.ACTION_BATTERY_CHANGED)){
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
int percent = (level*100)/scale;
Toast.makeText(context, "Current battery level: " + percent + "%", Toast.LENGTH_SHORT).show();
}
Upvotes: 0