Android Tutorial Background location tracking

Status
Not open for further replies.

Erel

Administrator
Staff member
Licensed User


Simple example of a foreground service that keeps the process running in the background. The current location is shown in the persistent notification.

The app starts at boot and theoretically should run all the time. It also schedules itself to run with StartServiceAt. This can help in cases where the OS kills the process.

Relevant example, based on this example, which plays music in the background: https://www.b4x.com/android/forum/threads/background-task.111106/#post-693336

There is a new requirement in Android 10+ devices.
In order for the location tracking to continue working in the background, if the user chooses the "allow while app is open" permission option we need to add this line to the manifest editor:
B4X:
SetServiceAttribute(Tracker, android:foregroundServiceType, "location")
It also requires installing "android sdk platform 29" with B4A Sdk Manager and configuring the path to android.jar under Tools - Configure Paths to android.jar from platform 29.
 

Attachments

Last edited:

Multiverse app

Active Member
Licensed User
Hi,
How much important is to call
B4X:
Private lock As PhoneWakeState
lock.PartialLock
How much more battery is consumed in the background after getting the partial lock?
Thanks.
 

Multiverse app

Active Member
Licensed User
Also,
B4X:
StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True)
This means the service is scheduled to start every 30 seconds. right?
 

Erel

Administrator
Staff member
Licensed User
Partial lock: If you want to try and prevent the CPU from sleeping then acquire this lock. It doesn't have any effect when the device is not sleeping.
This means the service is scheduled to start every 30 seconds. right?
No. 30 * DateTime.TicksPerMinute.
 

netsistemas

Active Member
Licensed User
Very important to do (don´t eliminate in sample):
B4X:
Service.StartForeground(nid,  CreateNotification("..."))
in Service_Start event of Tracker service

else tracker service are destroyed.
 
Last edited:

Multiverse app

Active Member
Licensed User
Also,
Just noticed that Service.StopForeground() is not called.
Does system handle it automatically, or is it just a programming error?
 

netsistemas

Active Member
Licensed User
I believe that is the right thing. If the service that handles the notifications stops, the rest of services will do it automatically, which would be a big problem. I suppose if you want to stop the trace service, you must close first the notification service (close Notificacion lanch in StartForeGrond).
Excuse me and rejoice in my explanations. I'm not an expert on these issues.
Android 8+ is more automatic: close service (service starter ignored, but no others)
 

Erel

Administrator
Staff member
Licensed User
Does system handle it automatically, or is it just a programming error?
Neither. This app is intended to always run so it is never stopped. Call StopService(<service name which I don't remember>) if you want to stop it.

BTW, it is already running for 5 days here without any interruption.
 

Christian García S.

Active Member
Licensed User
Hello @Erel,

I have a question, when you want to stop the service with StopService(Servicio) but you have:

B4X:
StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True)
The service will start again even when you stop service before, how I can do ? with variables ?

Thanks
 

Yeskay

Member
Licensed User
While compiling the above said MyLocation Project am receiving the following error
B4A Version: 8.80 BETA #1
Parsing code. (0.00s)
Building folders structure. (0.02s)
Compiling code. (0.01s)
Compiling layouts code. (0.00s)
Organizing libraries. (0.00s)
Generating R file. Error
ERROR: In <declare-styleable> FontFamilyFont, unable to find attribute android:fontVariationSettings
ERROR: In <declare-styleable> FontFamilyFont, unable to find attribute android:ttcIndex
 
Status
Not open for further replies.
Top