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

Updates:

- Project updated with targetSdkVersion = 34.
This update required several changes:
  1. New non-dangerous permission:
    B4X:
    AddPermission(android.permission.FOREGROUND_SERVICE_LOCATION)
    With the above change the service can be started from the activity and run theoretically forever. However if we also want to start tracking after the device is booted then we need to follow several additional steps:
  2. We need to receive the ACCESS_BACKGROUND_LOCATION permission. This is a special permission and it is granted in the settings app. We can only help the user get into the settings app - see Activity_Resume code.
  3. The Tracker service can no longer be started after boot directly. This is related to the fact that we must make it a foreground service and it is impossible to do without the ACCESS_BACKGROUND_LOCATION permission. So the solution is to add a Receiver that starts after boot (see manifest editor), checks whether we have the permission and then starts the service with a call to StartForegroundService sub. Note that this sub doesn't actually moves the service to the foreground state. Instead it tells the system that the target service will be moved to the foreground. This is something that is required since Android 8 and in previous cases it was possible to manage it internally.

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

Attachments

  • MyLocation.zip
    10.6 KB · Views: 543
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