在Android上捕获USB存储附加意图

时间:2015-06-22 19:13:06

标签: android android-intent broadcastreceiver

我一直试图从我的USB存储设备连接时获得意图而没有任何运气。我已将我的清单文件修改为包含

<intent-filter>
    <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
    <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
                    android:resource="@xml/device_filter" />
</intent-filter>

<intent-filter>
    <action android:name="android.hardware.usb.action.MEDIA_MOUNTED" />
    <meta-data android:name="android.hardware.usb.action.MEDIA_MOUNTED"
                    android:resource="@xml/device_filter" />
</intent-filter>

在活动中,在device_filter.xml文件中设置适当的VID / PID。活动本身有一个广播接收器

private static final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(action)) {
                Log.i("MyActivity", "USB Attach Intent Captured");
            }
            if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
                Log.i("MyActivity", "USB Detach Intent Captured");
            }
            if (UsbManager.ACTION_USB_ACCESSORY_ATTACHED.equals(action)) {
                Log.i("MyActivity", "Stuff");
            }
        }
    }; 

我根本没有看到BroadcastReceiver被调用,所以我不认为我的问题必然是我过滤错误的操作。我只是不知道上一步我错过了什么。

0 个答案:

没有答案