Android 使用额外的插入U获取usb电缆插入/输出事件不起作用
我的目的是在Preferences中保存Android设备usb/电源线的当前状态:已连接/已断开。从开发者的网站上,我看到获得这种状态有两种意图:动作\电源\连接/断开。因此,我使用了与《开发者》上发布的代码相同的代码: 在第节中,监控充电状态的变化 清单Android 使用额外的插入U获取usb电缆插入/输出事件不起作用,android,android-intent,usb,broadcastreceiver,battery,Android,Android Intent,Usb,Broadcastreceiver,Battery,我的目的是在Preferences中保存Android设备usb/电源线的当前状态:已连接/已断开。从开发者的网站上,我看到获得这种状态有两种意图:动作\电源\连接/断开。因此,我使用了与《开发者》上发布的代码相同的代码: 在第节中,监控充电状态的变化 清单 <receiver android:name=".PowerConnectionReceiver"> <intent-filter> <action android:name="android.i
<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>
当我插入/拔出USB电缆时,广播总是正确发送并在PowerConnectionReceiver中捕获,但结果总是相同(=电缆已插入)
我用Galaxy Nexus 4.1.1/4.2.1对它进行了测试-我在Toast中总是得到2xFALSE(chargePlug=FALSE,usbCharge=FALSE)
为什么intent.getIntExtra(BatteryManager.EXTRA_,-1)始终返回默认值“-1”?
多谢各位
附言:如果我用Java注册接收器,一切正常
Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
…在服务或活动中。但根据Android开发者网站的说法,让上面的代码返回正确的值并不是先决条件 我遇到你的帖子时,也遇到了同样的问题。我认为问题在于,您提供链接的Android培训页面中的代码是错误的 ACTION\u POWER\u CONNECTED和ACTION\u POWER\u DISCONNECTED没有“携带”您正在从代码中的意图查询的数据。我也不能让同样的东西在我的设备上运行(运行安卓4.3的第一代Nexus 7和运行安卓2.3.6的Nexus One),因为它似乎在任何地方都不起作用,我得出了“坏代码”的结论 我用下面的代码修复了它。它实际上非常接近您提供的修复程序,只是它直接进入我的广播接收器,而不是进入活动。在上面的代码中,这里的代码段正好位于“int chargePlug”开头的行之前。之后,将同一行(您的行)中从“int chargePlug”开始的“intent”更改为“minent” 请注意,您的BroadcastReceiver现在有两个意图。一个传递给它,它将包含连接的动作动作(或断开的动作)和在广播接收器内创建的另一个,以明确提取电池信息 如果您将我提供的代码段放入活动(它不会连接到您的BroadcastReceiver)中,则由于参数列表中的null,该代码段将无法工作。实际上,您并没有使用该空值注册BroadcastReceiver。将空值更改为BroadcastReceiver也不理想,因为操作电池更改通知可能会触发很多错误。我很有信心,更换的电池并不是用来触发广播接收机的。相反,我认为它是用来实时获取最后一次“粘性”广播,其中包含电池信息变化的信息(在Android文档中查找“粘性广播”)
还要注意,我给出的代码片段在调用链中包括“getApplicationContext()”。取出它,姜饼设备将崩溃(感谢Commonware)。我不确定您为什么会出现问题,但对我有效的方法是为已连接的操作和已断开的操作注册操作:
<receiver android:name="PowerReceiver" >
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>
我想知道这是否与这些问题有关这回答了你的问题吗?
final Intent mIntent = context.getApplicationContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
<receiver android:name="PowerReceiver" >
<intent-filter>
<action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
<action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
</intent-filter>
</receiver>
public class PowerReceiver extends BroadcastReceiver {
@Override public void onReceive(Context context, Intent intent) {
if(intent.getAction() == Intent.ACTION_POWER_CONNECTED) {
//Handle power connected
} else if(intent.getAction() == Intent.ACTION_POWER_DISCONNECTED){
//Handle power disconnected
}
}
}
public class PowerConnectionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final Intent mIntent = context.getApplicationContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int status = mIntent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
int chargePlug = mIntent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BATTERY_PLUGGED_AC;
}
}