Why does my receiver always return false for charging status?
I am trying to detect when a user connects (or disconnects) their device for charging. In my receiver, where I determine if it is connected or not, I always get a "false" status read. Here is my code:
(In the manifest):
<receiver android:name=".PowerConnectionReceiver" >
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>
Here is the PowerConnectionReceiver class:
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;
Log.d("Battery", "Plugged In: " + String.valueOf(isCharging));
Log.d("Battery", "status: " + String.valueOf(status));
}
}
What works: The PowerConnectionReceiver is called correctly when the phone is connected.
However, when I print out the status, it always returns as -1 (this is the default I entered). It seems BatteryManager.EXTRA_STATUS is not working properly.
For reference, here's what these logs print:
"Plugged In: false"
"status: -1"
More links - here is the developer site page I use to do this: http://developer.android.com/training/monitoring-device-state/battery-monitoring.html
source to share
Got this ... this answer came from another post on this site regarding another issue regarding battery.
public class PowerConnectionReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if(action.equals(Intent.ACTION_POWER_CONNECTED))
{
// Do code here for when power connected
}
else if(action.equals(Intent.ACTION_POWER_DISCONNECTED))
{
// Do code here for when power disconnected
}
}
While this answer is not entirely graceful, anyone who is reasonably familiar with accessing the other properties that come with this .getAction () intent should be able to do more with battery information. Otherwise, it does everything I need! Greetings.
source to share
I may be wrong, but it seems that the status is a collection of flags. For example, the battery may be fully charged and still connected to the charger. So what you can try, instead of comparing using the "==" operator, try applying a bitmask. Something like that:
boolean usbCharge = chargePlug & BatteryManager.BATTERY_PLUGGED_USB == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug & BatteryManager.BATTERY_PLUGGED_AC == BatteryManager.BATTERY_PLUGGED_AC;
From BatteryManager sources:
/** Power source is an AC charger. */
public static final int BATTERY_PLUGGED_AC = 1;
/** Power source is a USB port. */
public static final int BATTERY_PLUGGED_USB = 2;
/** Power source is wireless. */
public static final int BATTERY_PLUGGED_WIRELESS = 4;
/** @hide */
public static final int BATTERY_PLUGGED_ANY =
BATTERY_PLUGGED_AC | BATTERY_PLUGGED_USB | BATTERY_PLUGGED_WIRELESS;
So the power supply can be a superposition of bits
The same can be applied to a state variable:
boolean isCharging =
(status & BatteryManager.BATTERY_STATUS_CHARGING == BatteryManager.BATTERY_STATUS_CHARGING)
|| (status & BatteryManager.BATTERY_STATUS_FULL == BatteryManager.BATTERY_STATUS_FULL);
source to share