简体   繁体   中英

Android disable default app selection prompt when connect to a usb device

I have an Android app that need to connect to a Usb device. I have used intent filter to get permission to the device, however, every time when I connect phone with the usb device, Android would automatically prompt a selection for default apps that to open with the usb device. I know the all the default apps for the device are determined by activities that have intent filter <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />

So my question is, how can I disable the prompt for the device? No matter the app is open or not, the prompt always show up when device attached which is really annoying to me. Thanks for any help!

I just found there is a way to "hack" the prompt.

  1. Create a blank activity, add the intent-filter with USB attach filter <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />

  2. Remove all other activities's USB Attach intent filter, and add requestPermission manually in these activities. Because the blank activity would remember the USB permission for the whole app, so once any these activities grant the USB permission by user, then the whole app is granted. Because now only the blank activity has the USB attach intent filter registered, so this blank activity would be launched by default when USB device plugin

  3. In the blank activity onCreate , call onBackPressed so that once the blank activity is launched, it would automatically go back, and because this is really fast, so the user shouldn't realize any change.

Therefore, when the app is opened, plugin the usb device would just like nothing has happened to user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM