Android Question Help Needed, multiple instances of app launched after device rebooted

walterf25

Expert
Licensed User
Hello All, i'm in need of you guys helping me brainstorm in order to figure out this issue, I have an app i've made which uses the USB_HOST (HID) library, i'm communicating with a medical device, everything works fine except that while testing, one test requirement is to cycle power on the Android device, while having the usb unit plugged in.
The Android device reboots just fine, and the application gets launched automatically since the usb cable is plugged in and permission has already been granted.
The app also starts just fine and starts communicating with the medical device, the problem is that, it seems as if the operating system launches the app multiple times one after another, therefore confusing the communication between the android device and the medical device.
I also noticed that one of the timers is being fired up even before enabling it, this is not the first time i've noticed this behaviors with timers which is why i try to avoid them, but in this case i need to have a timer, in case there's a checksum error, i need to re-start the handshaking with the device from the beginning so i have a timer to wait for 5 seconds before re-starting communication again.
My Main problem right now is that the app re-starts multiple instances after the android device has been rebooted, I'm not sure why this is, but if anyone has any idea or has came up this same issue, i will really appreciate your help or thoughts on this. I've tried three different devices so far, and i get the same behavior. If I restart the android device without having medical device plugged in, and wait for the device to fully boot up, and then if i plug the usb device in, everything works just fine the way is supposed to, i'm not sure what the problem could be, maybe an operating system issue? I don't know, hopefully someone here has an answer.

Thanks all
Walter
 

Erel

B4X founder
Staff member
Licensed User
I never encountered the issue you mentioned with timers. It is impossible for a timer to raise the tick event if you haven't enabled it.

How do you see that the app has started multiple times? The service is allowed to be started multiple times. However Service_Create should only be called once (unless you stopped the service).
 
Upvote 0

walterf25

Expert
Licensed User
I never encountered the issue you mentioned with timers. It is impossible for a timer to raise the tick event if you haven't enabled it.

How do you see that the app has started multiple times? The service is allowed to be started multiple times. However Service_Create should only be called once (unless you stopped the service).
Hi Erel, the reason i even mentioned the timer is because i'm logging everything in a text file with a timestamp, when the main activity starts I initialize the communication with the medical device, i first wake up the device, and wait for response, once i get a response, i proceed to getting the device's model #, once i get a model # i proceed to getting the device's serial number, once i get the serial number i proceed to downloading the data inside the device.
When the android device is rebooted and the app starts automatically the main activity starts running but when i look at the log text file, i see no other log entries from anything else except from the timer, which tells me that when the android device was rebooted and the app was started the timer was fired up immediately.

The Reason i say the app gets started multiple instances is because i have a toast message in the main activity that shows this,
B4X:
ToastMessageShow("First time Activity Started : " & FirstTime, True)
the first time after the device has rebooted and the app starts I see "First time Activity Started : True", then a few seconds later I see "First time Activity Started : False", then a few seconds later I see the same message again "First time Activity Started : False" not only that but I can also see a toast message letting me know when data download has started, that toastmessage also shows the same amount of times as the toastmessage that lets me know whether is the first time the activity has started.

Hope this makes sense, and you or anyone else helps me find a solution to this issue, by the way i'am using only one service but it only gets called once the data has been downloaded and processed, and the service is not set to Start on Boot either.

Thanks,
Walter
 
Upvote 0

walterf25

Expert
Licensed User
This doesn't mean that the app was started multiple times. Why don't you work with Log instead of these toasts? It is much easier to understand what happens with logs.

It is possible that Android sends multiple intents to your activity. However it should cause any special issue.
The reason i can't use logs in this case is because i need to see what happens exactly when the app starts after the device has been rebooted, since the usb port is being used by the medical device i can't see the logs, and even if i use the B4A Bridge i can't start it as soon as the app starts after the device has been rebooted in order for me to see what's going on in the logs, is there another way to see the logs? Am I missing something?

You said that the fact that i see the toast message various times when i should only see it once because it is in the main activity, that doesn't mean that the app was started multiple instances, then why do I see this behavior? Why a line of code that is inside the main activity get executed multiple times when it shouldn't?

Thanks,
Walter
 
Upvote 0

sorex

Expert
Licensed User
Hey Walter,

toast are slow, maybe it's better to append it to a scrollable text box so that you can go back and see what happend?

somthing like


logme ("timer started")

sub logme(t)
logging.text=logging.text & t & chr(13) & chr(10)
end sub
 
Upvote 0

walterf25

Expert
Licensed User
Hey Walter,

toast are slow, maybe it's better to append it to a scrollable text box so that you can go back and see what happend?
Hey Sorex, yes I know Toasts are slow, but in this case the toastmessage should only show once when the app starts, there's no reason why it should show more than once, believe me i have been dealing with this issue for days now and have gone through the code and there's no other place in the code where the toastmessage line of code is.

Thanks,
Walter
 
Upvote 0

walterf25

Expert
Licensed User
do you see it running twice when you look around with an app killer tool?
I haven't tried that Sorex, the problem is that i can't do anything else after the device reboots, since the app starts right away as soon as the device reboots.
 
Upvote 0

walterf25

Expert
Licensed User
on what kind of device is this running that you can't control it?
Well i've tried on three different devices Sorex, I've tried it on my Galaxy S4, I've tried on two different Android Sticks MK908 and MK809, the reason the App starts automatically as soon as the devices reboot is because the USB is plugged in, and this is the default behavior when using the USB library, once the user grants permission to access and connect to the device the app will get launched as long as the USB cable is plugged in.
The Strange thing is that if I reboot the device first without having the USB cable plugged in, and wait for the android device to boot up and then plug in the USB cable everything works the way it should.
 
Upvote 0

Straker

Active Member
Licensed User
Probably my theory is wrong (I'm a newbie) but I think that at the device reboot there are many apps scheduled to start. So the Walter's app will start (firsttime true) and then another app starts and the previous app is sent to background and then back to foreground (firsttime false). It is not the same app started multiple times (multiple instances = multiple firsttime true) but the same app that switches from foreground to background.
 
Upvote 0

sorex

Expert
Licensed User
never used the USB lib so it's not clear to me how the system knows which app it has to start.

is your app running a boot service aswell?

did you try an app launcher instead so that the app launches at a later point without boot but regular service?
 
Upvote 0

walterf25

Expert
Licensed User
Probably my theory is wrong (I'm a newbie) but I think that at the device reboot there are many apps scheduled to start. So the Walter's app will start (firsttime true) and then another app starts and the previous app is sent to background and then back to foreground (firsttime false). It is not the same app started multiple times (multiple instances = multiple firsttime true) but the same app that switches from foreground to background.
Hey Straker, thanks for your thought, it actually makes sense, however the Toastmessage should only be executed from my app, and not from any other app.
 
Upvote 0

walterf25

Expert
Licensed User
never used the USB lib so it's not clear to me how the system knows which app it has to start.

is your app running a boot service aswell?

did you try an app launcher instead so that the app launches at a later point without boot but regular service?

Sorex with the USB library if you add this to the manifest File
B4X:
AddActivityText(Main, <intent-filter>
        <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
    </intent-filter>
    <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
        android:resource="@xml/device_filter" />)
In the Device_Filter xml file, i have the USB Vendor ID number and the Manufacturer ID #, this keeps the app from asking for usb permission every time the app is launched.

And No the app is not running a boot Service either.
 
Upvote 0

walterf25

Expert
Licensed User
Did you change the launchMode of the activity?
Does your manifest contain something else non standard?
I did change it to this
B4X:
SetActivityAttribute(main, android:launchMode, "singleInstance")
but it doesn't make a difference, prior to adding the above line to the manifest file i was getting the same results, i added this line in hopes that it would help, but no luck, even if i take out that line, the app still acts the same way.
 
Upvote 0
Top