Android Question How to prevent start activity when USB device is connected?

Jarek I&Q

Member
Licensed User
When I plug in a USB device, my application automatically starts (permissions are granted and remembered). How do I prevent this from happening?
Ideally, an application should start but remain in the background, with activity only visible when a user presses an icon.

However, I want all the permissions granted to be remembered to prevent the USB permission request dialog from appearing.
 
Last edited:

drgottjr

Expert
Licensed User
Longtime User
are you sure you don't have a broadcast action filter in your manifest? or some kind of intent filter?
even if the system itself is reacting to an alien device, it's curious that your app is singled out for
launch.
regarding runtime permissions, once granted or revoked, they are remembered unless the user
changes them in settings for your app or unless the app is uninstalled and re-installed. the only
exception, as i recall, is for location fine access, where the user can grant a 1-time permission,
in which case, permission would have to be granted the next tiime the app ran.
 
Upvote 0

Jarek I&Q

Member
Licensed User
are you sure you don't have a broadcast action filter in your manifest?
Thank you for your response.

My app is working correctly and has the correct filters - everything is ok with the USB device.
However, when this device is plugged in, my application automatically starts. This is the correct behaviour but I don't want this.

This is what I want to achieve: when connecting an already known USB device, my application should not start showing, but run in the background.
Only when a user launches the application, it should show its activity.

Thanks,
Jarek.
 
Last edited:
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
My app is working correctly and has the correct filters - everything is ok with the USB device.
However, when this device is plugged in, my application automatically starts. This is the correct behaviour but I don't want this.

app is doing exactly what you told it to do: "working correctly", "correct filters", "correct behaviour". we should all be so lucky :).

i am not familiar with paired usb devices. presumably you can figure out an ad hoc solution to recognizing if the device being plugged in was seen before.

i think you will ultimately be told (by someone higher up the food chain here) that the only way to achieve what you now want is to start the app that runs a foreground service. lots of threads here. if the app can run in the background, it's not clear why you even need to show its activity, but that's another story. the service would be where you attempt to identify if the usb device has been seen before. if so, continue running in background. otherwise, go to main.

in any case, you can no longer start an activity from the background without previously having run a foreground service.
this is where you start: https://www.b4x.com/android/forum/threads/background-location-tracking.99873/
 
Upvote 0

Jarek I&Q

Member
Licensed User
presumably you can figure out an ad hoc solution to recognizing if the device being plugged in was seen before
Thank you for the idea, but I don't feel like an expert to handle it easily.
So, I'll wait a bit before starting with your tips.

Maybe there are simpler ways like for example: hey, type in manifest - android:launchMode="singleInstance"
 
Upvote 0

Jarek I&Q

Member
Licensed User
The permission should only be asked once.
I apologize for not being able to explain well what I want to achieve, but I will try.

I just want one thing - to make an app not automatically appear in the foreground when a known USB device is connected.

My app behaves correctly, it remembers all permissions (it only asks permission once) and the USB device works fine.
A user sets something in the app and the USB device starts transmitting and receiving data.
The user starts another application and the USB device continues to run in the background.
But when there is a momentary failure of the USB device (for example, a shock causing a temporary lack of connection) and when the USB resumes,
the background application goes to the foreground. This is standard android behaviour for a known PID and VID of a USB device - I think so.

However, I would like to change this behavior so that the application remains in the background. How do I achieve this?
 
Upvote 0
Top