我怎样才能检测的Android 4.2 USB连接? [英] How can I detect usb connection in Android 4.2?
问题描述
我用 ACTION_MEDIA_MOUNTED
和 ACTION_MEDIA_UNMOUNTED
来检测 USB
在的Nexus 4连接
,但我不能接收任何广播信号。
I used ACTION_MEDIA_MOUNTED
and ACTION_MEDIA_UNMOUNTED
to detect USB
connection on Nexus 4
, but I cannot receive any broadcast signal.
下面是我的广播接收器code:
Here is my broadcast receiver code:
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
filter.addDataScheme("file");
debugReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (Intent.ACTION_MEDIA_MOUNTED.equals(action)) {
debugOn = true;
} else if (Intent.ACTION_MEDIA_UNMOUNTED.equals(action)) {
debugOn = false;
}
}
};
registerReceiver(debugReceiver, filter);
任何想法?我还搜查他人的问题;他们说,如果我想补充
Any ideas? I also searched others questions; they said if I add
filter.addDataScheme(文件);
我会得到信号,但我已经尝试过,并没有得到任何
I will get the signal, but I have tried and nothing was received.
推荐答案
使用 UsbManager.ACTION_USB_DEVICE_ATTACHED
和 UsbManager.ACTION_USB_DEVICE_DETACHED
你的意图过滤器。
Use UsbManager.ACTION_USB_DEVICE_ATTACHED
and UsbManager.ACTION_USB_DEVICE_DETACHED
for your intent filter.
要检查连接PC使用 Intent.ACTION_BATTERY_CHANGED
和的onReceive
intent.getInt (BatteryManager.EXTRA_PLUGGED)
,看看值为 BatteryManager.BATTERY_PLUGGED_USB
。
To check for connection to PC use Intent.ACTION_BATTERY_CHANGED
and onReceive
intent.getInt(BatteryManager.EXTRA_PLUGGED)
and see if the value is BatteryManager.BATTERY_PLUGGED_USB
.
这篇关于我怎样才能检测的Android 4.2 USB连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!