B Bernard Harris Member Licensed User Longtime User Feb 2, 2018 #1 I would like to create a background service on the mobile device so it runs upon boot up and records the GPS location of the device. Is there a way within B4A to set the app to start at boot and also to check if it's running from another app and start if not? The background app just needs to run every 30 minutes to report the location and wait until the 30 minutes expire. I've reviewed the StartServiceAt but don't see a clear way to create as a separate app and kick this off on boot up.
I would like to create a background service on the mobile device so it runs upon boot up and records the GPS location of the device. Is there a way within B4A to set the app to start at boot and also to check if it's running from another app and start if not? The background app just needs to run every 30 minutes to report the location and wait until the 30 minutes expire. I've reviewed the StartServiceAt but don't see a clear way to create as a separate app and kick this off on boot up.
udg Expert Licensed User Longtime User Feb 2, 2018 #2 Add a new service (please not use the Starter service ) to your project and set its attributes like: Code: Copy #Region Service Attributes #StartAtBoot: True #StartCommandReturnValue: android.app.Service.START_STICKY #End Region In Service_Create, use Code: Copy StartServiceAt("", DateTime.Now + (30 * DateTime.TicksPerMinute), True) to program next reading 30 minutes from now. If you need to save GPS data to a remote server or plan to perform a long lasting action, consider reading about PartialLock. Last edited: Feb 2, 2018 Upvote 0
Add a new service (please not use the Starter service ) to your project and set its attributes like: Code: Copy #Region Service Attributes #StartAtBoot: True #StartCommandReturnValue: android.app.Service.START_STICKY #End Region In Service_Create, use Code: Copy StartServiceAt("", DateTime.Now + (30 * DateTime.TicksPerMinute), True) to program next reading 30 minutes from now. If you need to save GPS data to a remote server or plan to perform a long lasting action, consider reading about PartialLock.
B Bernard Harris Member Licensed User Longtime User Feb 5, 2018 #3 I created a separate app with a new service module and added this code. I then put into the Service Start to send a message, just so I can see that it's working first. I am not getting any message to show. I still needed the main module so that I could name the project, does that matter? It appears I have it running and I didn't restart the device to make sure it started during bootup. Any suggestions on why this wouldn't work? Upvote 0
I created a separate app with a new service module and added this code. I then put into the Service Start to send a message, just so I can see that it's working first. I am not getting any message to show. I still needed the main module so that I could name the project, does that matter? It appears I have it running and I didn't restart the device to make sure it started during bootup. Any suggestions on why this wouldn't work?
B Bernard Harris Member Licensed User Longtime User Feb 5, 2018 #4 I created this service module but how does it know to run the service module? Do I have to kick this off from the Main module then? Upvote 0
I created this service module but how does it know to run the service module? Do I have to kick this off from the Main module then?
KMatle Expert Licensed User Longtime User Feb 5, 2018 #5 Bernard Harris said: Do I have to kick this off from the Main module then? Click to expand... Yes. You must start it by code (via activity). The idea is to let the user know (= start an app) what happens on his device. An service is like a "sub". You start it by code and it executes some code. If you want that every x time, you must schedule it with "StartServiceAt" like udg posted. Android takes care that it will be started then. It's not that accurate. Search the forum for other possibilities or changes from Android 8 or so. Upvote 0
Bernard Harris said: Do I have to kick this off from the Main module then? Click to expand... Yes. You must start it by code (via activity). The idea is to let the user know (= start an app) what happens on his device. An service is like a "sub". You start it by code and it executes some code. If you want that every x time, you must schedule it with "StartServiceAt" like udg posted. Android takes care that it will be started then. It's not that accurate. Search the forum for other possibilities or changes from Android 8 or so.
B Bernard Harris Member Licensed User Longtime User Feb 5, 2018 #6 Okay, I think that worked. But in the service to test the process I had a Msgbox set to show in Service_Start. This causes the app to crash. Is it because I'm trying to display a Msgbox within the service itself? Upvote 0
Okay, I think that worked. But in the service to test the process I had a Msgbox set to show in Service_Start. This causes the app to crash. Is it because I'm trying to display a Msgbox within the service itself?
Ricky D Well-Known Member Licensed User Longtime User Feb 6, 2018 #7 Bernard Harris said: Okay, I think that worked. But in the service to test the process I had a Msgbox set to show in Service_Start. This causes the app to crash. Is it because I'm trying to display a Msgbox within the service itself? Click to expand... Yes. Use toastmessage instead Services dont have a ui Upvote 0
Bernard Harris said: Okay, I think that worked. But in the service to test the process I had a Msgbox set to show in Service_Start. This causes the app to crash. Is it because I'm trying to display a Msgbox within the service itself? Click to expand... Yes. Use toastmessage instead Services dont have a ui
B Bernard Harris Member Licensed User Longtime User Feb 6, 2018 #8 Okay, that worked. Now to test it when I reboot. Upvote 0