Android Question Startup Background app to record location

Bernard Harris

Member
Licensed User
Longtime User
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
Add a new service (please not use the Starter service ) to your project and set its attributes like:
B4X:
#Region  Service Attributes
    #StartAtBoot: True
    #StartCommandReturnValue: android.app.Service.START_STICKY
#End Region

In Service_Create, use
B4X:
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:
Upvote 0

Bernard Harris

Member
Licensed User
Longtime User
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

Bernard Harris

Member
Licensed User
Longtime User
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

KMatle

Expert
Licensed User
Longtime User
Do I have to kick this off from the Main module then?

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

Member
Licensed User
Longtime User
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

Ricky D

Well-Known Member
Licensed User
Longtime User
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?
Yes. Use toastmessage instead Services dont have a ui
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…