6

I have an application that is launched via an intent-filter action. The problem is that every time the event/ action occurs, Android displays a dialog asking to launch the app even if it is already started.

I want the behavior to be as follows:

  • User asked to launch the app if app is not open.
  • Dialog does not display if app is running in the foreground.

Is there a way to achieve both of these goals? I am targeting Android 4.0.

edit

Here is my intent filter:

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
        </intent-filter>

       <intent-filter>
            <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
        </intent-filter>

       <meta-data
            android:name="android.hardware.usb.action.USB_DEVICE_DETACHED"
            android:resource="@xml/device_filter" />
Brian
  • 6,910
  • 8
  • 44
  • 82

2 Answers2

10

You need to set the Activity's launchMode to singleTask. You can do this by adding the activity attribute in your Android Manifest:

android:launchMode="singleTask"

See the documentation for more details.

Phil
  • 35,852
  • 23
  • 123
  • 164
  • This solves half the problem!The app isn't started twice any more, but the dialog still appears every time the device connects/disconnects. Maybe this is just how things have to function if I want the auto-launching ability in there. – Brian Mar 04 '13 at 16:57
  • @brianv is it a chooser dialog? It may be that no default app is set. – Phil Mar 04 '13 at 17:10
  • When I connect the device, the dialog appears. On the top of the dialog is my app's icon along with the name. The message in the box says "Open AppName when this USB device is connected?" and then there is a check box "Use by default for this USB device" – Brian Mar 04 '13 at 17:15
  • I'll try making another app and see if I get a chooser dialog if I have more than one app with the same device filter... – Brian Mar 04 '13 at 17:17
  • Just created a stub app with the same device filter and intents and now I get a box to select between apps. I am thinking I may just have to deal with this as a part of the OS. – Brian Mar 04 '13 at 17:25
  • yeah, the chooser is definitely a user preference thing. I would assume the usb popoup is as well. – Phil Mar 04 '13 at 17:29
  • 1
    And the [documentation](http://developer.android.com/guide/topics/connectivity/usb/accessory.html) is pretty vague on when a popup is shown. @BrianV, keep an eye on [this post](http://stackoverflow.com/questions/12388914/usb-device-access-pop-up-supression/15151075#15151075), which asks the same question (second part of your question) and is up for bounty. – Phil Mar 04 '13 at 17:36
  • Even more strange is that when I get the chooser dialog, I don't get the OK/Cancel buttons on the bottom. – Brian Mar 04 '13 at 18:22
  • I've tried this solution except my app still restarts when the user requests that the app starts when the device is plugged in. Is there anything else that needs to be set for this solution to work? Thanks in advance. – Micah Simmons Feb 19 '17 at 12:40
0

Use flags when asking the user to start the application. Set the flag when this is first enabled and each time check if this flag is ticked. For example if (flag == 1) askusertostart() else continue; if this is clear to you?

g00dy
  • 6,752
  • 2
  • 30
  • 43
  • Not sure. I want to prevent the dialog from being shown in the first place, not simply not re-launch the app. – Brian Mar 04 '13 at 16:26
  • I am not invoking the dialog, the Android operating system is doing it when the USB device is plugged in. It is doing that (correctly) based on the intent filter and the values supplied in the device_filter. It's just doing it regardless of whether the app is running or not and this is what I need to change. Granting the permission to use the device can be done manually on an ad-hoc basis from within the activity, but I still want the dialog to show up when the device is plugged in. – Brian Mar 04 '13 at 16:30
  • I think that you should also pair the `USB_DEVICE_ATTACHED` in the `` tag. See if this helps somehow. – g00dy Mar 04 '13 at 16:38