我需要能够在基于MTP的Android设备上检测USB连接,即插入和断开连接时。这是我目前的相关代码,它基于http://developer.android.com/guide/topics/connectivity/usb/host.html
的manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.waracle.ncr"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="17" />
<uses-feature
android:name="android.hardware.usb.host"
/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.TestActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter"/>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" android:resource="@xml/device_filter"/>
</intent-filter>
</activity>
</application>
</manifest>
device_filter.xml。我试过这个没有值和值,我发现使用UsbManager连接所有USB设备
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-device
vendor-id="5401" product-id="32"
/>
</resources>
在我的onCreate中,我注册了广播接收器。
IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
registerReceiver(mUsbReceiver, filter);
这是广播接收器
private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Toast.makeText(TestActivity.this, "Test broadcastreceiver Received something", Toast.LENGTH_SHORT).show();
}
};
如果收到任何东西,这应该给我一个祝酒词,没有任何事情发生。
我一直在寻找解决方案,但我找到的所有工作都是基于UMS的。如果有人能帮忙解决这个问题会很棒,谢谢。