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:
- New non-dangerous permission:
B4X:AddPermission(android.permission.FOREGROUND_SERVICE_LOCATION)
- 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.
- 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
Last edited: