Can't receive broadcast Intent of UsbManager.ACTION_USB_DEVICE_ATTACHED/UsbManager.ACTION_USB_DEVICE_DETACHED

大城市里の小女人 提交于 2019-12-05 03:39:41

Perhaps a bit late, but it may help others. Just solved a similar problem with detecting the insertion of USB devices. It turns out that - because you specified an intent filter in the manifest - Android calls onResume when something is plugged in. You might try adding this:

@Override
protected void onResume() {
    super.onResume();

    Intent intent = getIntent();
    if (intent != null) {
        Log.d("onResume", "intent: " + intent.toString());
        if (intent.getAction().equals(UsbManager.ACTION_USB_DEVICE_ATTACHED)) {
            // Do your thing ...
        }

Then you also do not need the registerReceiver() call in onCreate(). Please also keep in mind that the ID's in the intent filter are in decimal. So you would have to convert the values as presented by command line tools like 'lsusb'.

The receiver tag is commented out, I'm guessing you know that but just incase. Also it should be declared as <receiver android:name="mUsbReceiver"> yours has a '.' which doesn't need to be there

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!