UsbActivity.this 改为 context 试试?

解决方案 »

  1.   

    filter.addAction(UsbManager.ACTION_USB_STATE);
      

  2.   

    经过测试, 只能收到usb拔出的事件。
    下面的链接是监听usb设备的文章
    http://blog.csdn.net/s278777851/article/details/6956226
      

  3.   


    这个是我根据这个网址相关的API写出的程序
    UsbAccessory accessory=(UsbAccessory)getIntent().getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
         Log.d("+++++++++++","accessory="+accessory);
         UsbDevice device=(UsbDevice) getIntent().getParcelableExtra(UsbManager.EXTRA_DEVICE);
            Log.d("+++++++++++","device="+device);
            if(device!=null){
             receiver=new UsbBroadcastReceiver();
             Intent intent=new Intent("android.hardware.usb.action.USB_DEVICE_ATTACHED");
             intent.addCategory("android.hardware.usb.action.USB_DEVICE_DETACHED");
             PendingIntent mIntent=PendingIntent.getBroadcast(this, 0, intent, 0);
            
             IntentFilter filter = new IntentFilter();            
                    filter.addAction("android.hardware.usb.action.USB_DEVICE_ATTACHED");
                    filter.addAction("android.hardware.usb.action.USB_DEVICE_DETACHED");
                    Usb12Activity.this.registerReceiver(receiver, filter);            
         
                    UsbManager usbManager=(UsbManager) this.getSystemService(Context.USB_SERVICE);
                
                    usbManager.requestPermission(device, mIntent);
            }xml文件:
    <?xml version="1.0" encoding="utf-8"?>
    <manifest ...>
        <uses-sdk android:minSdkVersion="12" />
        
        <uses-feature android:name="android.hardware.usb.host" ></uses-feature>    
        <uses-permission android:name="android.permission.HARDWARE_TEST"/>    <application ...>
            <activity ...>
                <intent-filter>
                   ...         
                </intent-filter>
                   
                <intent-filter>
                    <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"/>
                    <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED"/> 
                </intent-filter>
                <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_ATTACHED" android:resource="@xml/device_filter"/>
                
            </activity>
    xml/device_filter:
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
       <usb-device vendor-id="1234" product-id="5678" class="255" subclass="66" protocol="1" ></usb-device>
    </resources>
    结果是device一直为空,加了while循环不停判断,还一直为null。
           
        </application>
           
    </manifest>
      

  4.   

    <action android:name="android.hardware.usb.action.USB_STATE"/>这个filter呢?
      

  5.   

    mounted和unmounted是说媒体设备挂载和卸载。不代表usb插拔。
    usb可以使媒体设备,也可以是其实它设备。
    usb做为媒体设备时,mounted消息也不一定就会发出来,因为这需要驱动,还有vold进程,以及MountService这几个地方都设置想要的配置。才会把mounted的Intent发出来。插拔还是要看
    <action android:name="android.hardware.usb.action.USB_STATE"/>,
    但你的版本不允许。就没办法了。版本的问题,可以参看:
    http://developer.android.com/guide/topics/usb/index.html