android监听usb拔插事件 UsbActivity.this 改为 context 试试? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 filter.addAction(UsbManager.ACTION_USB_STATE); 经过测试, 只能收到usb拔出的事件。下面的链接是监听usb设备的文章http://blog.csdn.net/s278777851/article/details/6956226 这个是我根据这个网址相关的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> <action android:name="android.hardware.usb.action.USB_STATE"/>这个filter呢? 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 有人做过三星的odroid平台的方案吗?help!!!! android源码发送AT指令 PhoneStateListener.onSignalStrengthsChanged\ 在待机的时候不工作 关于 SimpleAdapter 中数据刷新的问题 开办啦,太好啦 安卓开发activity跳转的问题。 怎么可以实现连续动画animation?看要求: android开发,自定义ViewGroup的传参问题 在适配器中进行页面跳转后,如何讲适配器所属页面从页面所属栈中清除 ExpandableListView屏蔽长按事件 求一个android上面接收邮件的程序 android webdav
下面的链接是监听usb设备的文章
http://blog.csdn.net/s278777851/article/details/6956226
这个是我根据这个网址相关的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>
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