Android Question [SOLVED] Background Location Tracking Turn on/off in app

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
I have a program where I want the user to have 3 options:
  1. No location Tracking
  2. Location tracking whilst the app is foreground
  3. Location tracking with the app in background

I can get 1 & 3 working by using the code here:
and using StartService and StopService.

2 is the what I have a prbolem with. Reading through the comments to this post the suggestion is that the lines:
B4X:
    Service.StartForeground(nid, CreateNotification("..."))
and in the manifest
B4X:
SetServiceAttribute(Tracker, android:foregroundServiceType, "location")

allow the background tracking.

How can I turn off the service Attribute programmtically so that the user can turn off background tracking but still have tracking whilst the app is active?

Cheers
 
Solution
I will try @Sandman 's suggestion for foreground only tracking.
I think I understand what you are looking for...

If you want to actually prevent any of your tracking code from running when the user only wants foreground tracking, then when your app activity is paused, add code in the pause event that will check to see if the user selected "Foreground Only Tracking" and if so, then stop the background GPS service module and clear any future scheduled service tasks for it using CancelScheduledService(ServiceName). This will prevent the GPS service from running when the app is not in the foreground.

Then when your activity resumes, restart the GPS service.

JohnC

Expert
Licensed User
Longtime User
Luckily #2 (foreground tracking) is easier to achieve than #3, so that's a good problem to have.

But there are a few different things going on that are could be complicating the issue:

When an app wants to track GPS (and the app uses background tracking), android will give the user these runtime permission options:
  • Don't allow tracking at all
  • Only allow tracking when app is in foreground
  • Allow tracking in background
The service in the example you posted should be able to be used for both background and foreground tracking (if the user granted background tracking).

So, are you saying that if the user selects "Only allow tracking when app is in foreground", that the service (in the example) is not properly running?

Also when you say "How can I turn off the service Attribute programmtically so that the user can turn off background tracking but still have tracking whilst the app is active?" that makes me think that you might be thinking that the service is only needed for background tracking. This I believe is not true. The service can run when the app is in the foreground and when the app goes to background android will simply not allow your app to get GPS info in the background if the user denied that runtime permission. So basically, you need the service in the example to run regardless if the app is running in the foreground or background.
 
Last edited:
Upvote 0

Andrew (Digitwell)

Well-Known Member
Licensed User
Longtime User
For 2, wouldn't it just work to enable the GPS and get the events with locations from it?
I didn't think of just using GPS. I will try that for option 2.

Luckily #2 (foreground tracking) is easier to achieve than #3, so that's a good problem to have.

But there are a few different things going on that are could be complicating the issue:

When an app wants to track GPS (and the app uses background tracking), android will give the user these runtime permission options:
  • Don't allow tracking at all
  • Only allow tracking when app is in foreground
  • Allow tracking in background
The service in the example you posted should be able to be used for both background and foreground tracking (if the user granted background tracking).

So, are you saying that if the user selects "Only allow tracking when app is in foreground", that the service (in the example) is not properly running?

Also when you say "How can I turn off the service Attribute programmtically so that the user can turn off background tracking but still have tracking whilst the app is active?" that makes me think that you might be thinking that the service is only needed for background tracking. This I believe is not true. The service can run when the app is in the foreground and when the app goes to background android will simply not allow your app to get GPS info in the background if the user denied that runtime permission. So basically, you need the service in the example to run regardless if the app is running in the foreground or background.
@JohnC , Yes, the service does run when the app is in the foreground as well as background and works properly.

If the user selected option 2, then I want to stop background tracking and only track whilst the app is active. I will try @Sandman 's suggestion for foreground only tracking.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I will try @Sandman 's suggestion for foreground only tracking.
I think I understand what you are looking for...

If you want to actually prevent any of your tracking code from running when the user only wants foreground tracking, then when your app activity is paused, add code in the pause event that will check to see if the user selected "Foreground Only Tracking" and if so, then stop the background GPS service module and clear any future scheduled service tasks for it using CancelScheduledService(ServiceName). This will prevent the GPS service from running when the app is not in the foreground.

Then when your activity resumes, restart the GPS service.
 
Upvote 1
Solution
Top