Nov 29, 2018 Replies: 15 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: Code: 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: 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 MyLocation.zip MyLocation.zip 10.6 KB · Views: 1,942 Last edited: Jul 3, 2024 Click to expand...
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: Code: 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: 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.
Multiverse app Active Member Licensed User Longtime User Dec 2, 2018 #2 Hi, How much important is to call Code: Private lock As PhoneWakeState lock.PartialLock How much more battery is consumed in the background after getting the partial lock? Thanks.
Hi, How much important is to call Code: 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 Dec 2, 2018 #3 Also, Code: StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True) This means the service is scheduled to start every 30 seconds. right?
Also, Code: 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 Dec 2, 2018 #4 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. Multiverse app said: This means the service is scheduled to start every 30 seconds. right? Click to expand... No. 30 * DateTime.TicksPerMinute.
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. Multiverse app said: This means the service is scheduled to start every 30 seconds. right? Click to expand... No. 30 * DateTime.TicksPerMinute.
netsistemas Active Member Licensed User Longtime User Dec 3, 2018 #5 Very important to do (don´t eliminate in sample): Code: Service.StartForeground(nid, CreateNotification("...")) in Service_Start event of Tracker service else tracker service are destroyed. Last edited: Dec 8, 2018
Very important to do (don´t eliminate in sample): Code: Service.StartForeground(nid, CreateNotification("...")) in Service_Start event of Tracker service else tracker service are destroyed.
Multiverse app Active Member Licensed User Longtime User Dec 3, 2018 #6 Also, Just noticed that Service.StopForeground() is not called. Does system handle it automatically, or is it just a programming error?
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 Dec 3, 2018 #7 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)
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 Dec 4, 2018 #8 Multiverse app said: Does system handle it automatically, or is it just a programming error? Click to expand... 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.
Multiverse app said: Does system handle it automatically, or is it just a programming error? Click to expand... 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.
Erel B4X founder Staff member Licensed User Longtime User Dec 4, 2018 #9 netsistemas said: Very important to do: Click to expand... This is already implemented in the example.
netsistemas said: Very important to do: Click to expand... This is already implemented in the example.
C Christian García S. Active Member Licensed User Dec 8, 2018 #10 Hello @Erel, I have a question, when you want to stop the service with StopService(Servicio) but you have: Code: 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
Hello @Erel, I have a question, when you want to stop the service with StopService(Servicio) but you have: Code: 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
Erel B4X founder Staff member Licensed User Longtime User Dec 9, 2018 #11 Good question. You also need to call: Code: CancelScheduledService(Me)
sentechnologies Member Licensed User Dec 23, 2018 #12 While compiling the above said MyLocation Project am receiving the following error Spoiler: 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
While compiling the above said MyLocation Project am receiving the following error Spoiler: 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
DonManfred Expert Licensed User Longtime User Dec 23, 2018 #13 Yeskay said: While compiling the above Click to expand... 1. You should always start a new thread for your questions. 2. Use the b4a sdk manager and update all recommeded items. 3. Use Android.jar from Platform 28
Yeskay said: While compiling the above Click to expand... 1. You should always start a new thread for your questions. 2. Use the b4a sdk manager and update all recommeded items. 3. Use Android.jar from Platform 28
Erel B4X founder Staff member Licensed User Longtime User Nov 27, 2022 #14 I've updated the example and removed the StartServiceAt call as it is no longer allowed to start services when the app is in the background (except of a few special cases).
I've updated the example and removed the StartServiceAt call as it is no longer allowed to start services when the app is in the background (except of a few special cases).
Erel B4X founder Staff member Licensed User Longtime User Jul 16, 2023 #15 - Project updated with targetSdkVersion = 33 and the handling of the new post notifications permission.
- Project updated with targetSdkVersion = 33 and the handling of the new post notifications permission.
Erel B4X founder Staff member Licensed User Longtime User Jul 3, 2024 #16 Project updated with targetSdkVersion = 34. See first post for more details.