问题
I'm trying to detect when a user plugs (or unplugs) in their device to charge. In my receiver, where I determine whether it is plugged in or not, I always get a "false" reading on the status. 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's working: The PowerConnectionReceiver properly gets called when the phone is plugged in.
However, when I print out the status, it always returns as -1 (which is the default value I entered in). It seems the BatteryManager.EXTRA_STATUS isn't coming in properly.
For reference, here is what those Logs are printing out:
"Plugged In: false"
"status: -1"
More reference - here is the page on the Developers site I am using for this: http://developer.android.com/training/monitoring-device-state/battery-monitoring.html
回答1:
Got it... this answer came from another post on this site regarding a different issue regarding the 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
}
}
Although this answer isn't totally elegant, anyone familiar enough with how to access other properties that come along with that intent.getAction() should be able to do more with the battery information. Otherwise, this does everything I need it to do! Cheers.
回答2:
I may be wrong, but it seems that status represents a set of flags. For example, battery can be fully charged and still connected to the charger. So what you can try is instead of comparing using "==" operator, try to apply bitmask. Something like this:
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 power source may be a superposition of bits
The same may be applicable to status variable:
boolean isCharging =
(status & BatteryManager.BATTERY_STATUS_CHARGING == BatteryManager.BATTERY_STATUS_CHARGING)
|| (status & BatteryManager.BATTERY_STATUS_FULL == BatteryManager.BATTERY_STATUS_FULL);
来源:https://stackoverflow.com/questions/29765146/why-is-my-receiver-for-charging-status-always-returning-false