2

Events:

1.USB device plugged in.

  1. Prompt appears saying: "Open appName when this USB device is connected?"
  2. Press cancel -> app stays open | press okay -> app restarts

Required behaviour: The app either does not show the dialog and automatically opens the app when it's not in the foreground, or, when the app is in the foreground then when the user presses okay the app does not restart.

Attempted solution: I've tried making sure every Activity's launch mode is set to "SingleTask". This solution worked here but when I did the same thing the app still restarted.

Question in a nutshell: How do I stop my app restarting when the user says to open the app when the device is connected? Or, how do I stop this prompt entirely whilst still enabling my app to start when this USB device is connected?

I've seen similar questions which deal with stopping a prompt showing which asks whether the app should be allowed to access the connected device, but, my issue is dealing with a different prompt.

Thanks in advance.

Community
  • 1
  • 1
Micah Simmons
  • 2,078
  • 2
  • 20
  • 38

1 Answers1

0

the app uses an IntentFilter with an AppChooser or Launcher dialog https://developer.android.com/guide/components/intents-filters.html

this always happens when you use an IntentFilter on android.hardware.usb.action.USB_DEVICE_ATTACHED. Another possibility is that the dialog is an AppChooser or launcher dialog that is invoked when there are multiple apps (or multiple instances of the same / your app) that capture the USB attach event . This is why when you press Cancel the app stays open.

Using an intent filter

To have your application discover a particular USB device, you can specify an intent filter to filter for the android.hardware.usb.action.USB_DEVICE_ATTACHED intent. Along with this intent filter, you need to specify a resource file that specifies properties of the USB device, such as product and vendor ID. When users connect a device that matches your device filter, the system presents them with a dialog that asks if they want to start your application. If users accept, your application automatically has permission to access the device until the device is disconnected.

https://developer.android.com/guide/topics/connectivity/usb/host.html

this is a systems dialog that is internally linked to the android.hardware.usb.action.USB_DEVICE_ATTACHED event so disabling it will be difficult. there is the possibility to use <activity-alias>, https://developer.android.com/guide/topics/manifest/activity-alias-element.html you can route intents with it:

http://www.stackoverflow.com/questions/40182096/usb-permissions-without-prompt/40182413#40182413

http://blog.danlew.net/2014/01/16/preserve-your-launchers-use-activity-alias/ (Is there any way to have one and only one instance of each activity?)

Community
  • 1
  • 1
ralf htp
  • 9,149
  • 4
  • 22
  • 34
  • Thanks for your response. What part of my code would you need to say how to disable the dialog? The dialog is automatically generated by Android. The second link that you provided gives me basically the same solution as the one that I said that I had tried in my question (it suggests using: `SingleInstance` & I said that I used `SingleTask`) and I think that both modes don't stop the app restarting. – Micah Simmons Feb 20 '17 at 09:39
  • post `manifest.xml` and anything you do with the `IntentFilter` for `USB_DEVICE_ATTACHED` – ralf htp Feb 20 '17 at 11:10