为什么我的充电状态接收器总是返回错误?
Why is my Receiver for charging status always returning false?
我正在尝试检测用户何时插入(或拔出)他们的设备进行充电。在我的接收器中,我确定它是否已插入,我总是得到 "false" 状态读数。这是我的代码:
(在清单中):
<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>
这是 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));
}
}
工作原理:插入 phone 时会正确调用 PowerConnectionReceiver。
然而,当我打印状态时,它总是returns为-1(这是我输入的默认值)。 BatteryManager.EXTRA_STATUS 似乎没有正确进入。
作为参考,这些日志打印出来的内容如下:
"Plugged In: false"
"status: -1"
更多参考 - 这是我为此使用的开发者网站上的页面:
http://developer.android.com/training/monitoring-device-state/battery-monitoring.html
我可能错了,但状态似乎代表一组标志。例如,电池可以充满电但仍连接到充电器。因此,您可以尝试使用位掩码而不是使用“==”运算符进行比较。像这样:
boolean usbCharge = chargePlug & BatteryManager.BATTERY_PLUGGED_USB == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug & BatteryManager.BATTERY_PLUGGED_AC == BatteryManager.BATTERY_PLUGGED_AC;
来自 BatteryManager 来源:
/** 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;
所以电源可能是比特的叠加
同样适用于状态变量:
boolean isCharging =
(status & BatteryManager.BATTERY_STATUS_CHARGING == BatteryManager.BATTERY_STATUS_CHARGING)
|| (status & BatteryManager.BATTERY_STATUS_FULL == BatteryManager.BATTERY_STATUS_FULL);
知道了...此答案来自本网站上另一个 post 关于电池的不同问题。
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
}
}
虽然这个答案并不完全优雅,但任何足够熟悉如何访问 intent.getAction() 附带的其他属性的人都应该能够对电池信息做更多的事情。否则,这就是我需要它做的一切!干杯。
我正在尝试检测用户何时插入(或拔出)他们的设备进行充电。在我的接收器中,我确定它是否已插入,我总是得到 "false" 状态读数。这是我的代码:
(在清单中):
<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>
这是 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));
}
}
工作原理:插入 phone 时会正确调用 PowerConnectionReceiver。
然而,当我打印状态时,它总是returns为-1(这是我输入的默认值)。 BatteryManager.EXTRA_STATUS 似乎没有正确进入。
作为参考,这些日志打印出来的内容如下:
"Plugged In: false"
"status: -1"
更多参考 - 这是我为此使用的开发者网站上的页面: http://developer.android.com/training/monitoring-device-state/battery-monitoring.html
我可能错了,但状态似乎代表一组标志。例如,电池可以充满电但仍连接到充电器。因此,您可以尝试使用位掩码而不是使用“==”运算符进行比较。像这样:
boolean usbCharge = chargePlug & BatteryManager.BATTERY_PLUGGED_USB == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug & BatteryManager.BATTERY_PLUGGED_AC == BatteryManager.BATTERY_PLUGGED_AC;
来自 BatteryManager 来源:
/** 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;
所以电源可能是比特的叠加
同样适用于状态变量:
boolean isCharging =
(status & BatteryManager.BATTERY_STATUS_CHARGING == BatteryManager.BATTERY_STATUS_CHARGING)
|| (status & BatteryManager.BATTERY_STATUS_FULL == BatteryManager.BATTERY_STATUS_FULL);
知道了...此答案来自本网站上另一个 post 关于电池的不同问题。
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
}
}
虽然这个答案并不完全优雅,但任何足够熟悉如何访问 intent.getAction() 附带的其他属性的人都应该能够对电池信息做更多的事情。否则,这就是我需要它做的一切!干杯。