Android Tutorial Background location tracking

Status
Not open for further replies.
SS-2018-11-29_15.48.38.png


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")

Updates:
- Project updated with targetSdkVersion = 33 and the handling of the new post notifications permission.
 

Attachments

  • MyLocation.zip
    9.3 KB · Views: 834
Last edited:

Multiverse app

Active Member
Licensed User
Longtime 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
Longtime User
Also,
B4X:
StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True)
This means the service is scheduled to start every 30 seconds. right?
 

Erel

B4X founder
Staff member
Licensed User
Longtime 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
Longtime 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
Longtime 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
Longtime 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

B4X founder
Staff member
Licensed User
Longtime 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.
 

sentechnologies

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