Android Question Background location tracking

Ralph Parkhurst

Member
Licensed User
Longtime User
I am learning about background GPS/GNSS using the Background location tracking example here: https://www.b4x.com/android/forum/threads/99873/#content

This works very well on my Android 12 device (Motorola G51) and location continues to be displayed when the app is in the background.

However, I cannot display locations using "run at boot" which I've assumed that the location will start being notified after the device has booted. After reading elsewhere in this forum, I now understand that "RunAtBoot: True" is no longer supported within a service. and I need to update to a Receiver module instead.

So I have tried to adapt the Tracker service module to a Receiver module instead, although so far without success.

Is this the correct approach? If someone provide guidance I would be enormously appreciative.

Many thanks,
Ralph
 

Sandman

Expert
Licensed User
Longtime User
Are you 100% sure that the starter isn't being started after boot?
Also: Please note that some devices take their sweet time to start the app after boot. It's been some time since I used this, but I think I saw it delayed at least five minutes or so.

(I haven't read anything about the cause for this, but I imagine there are historical reasons, where 100 apps instantly autostarting after boot made the device super slow, which affected user opinions about the device and Android OS itself.)
 
Upvote 0

Ralph Parkhurst

Member
Licensed User
Longtime User
Are you 100% sure that the starter isn't being started after boot?
That's a good question Erel. I will check again tomorrow when I am back at work = however I recall that the RunAtBoot must indeed be working, because this line is executed at boot time:

B4X:
Service.StartForeground(nid, CreateNotification("..."))

I can actually see the "..." on the notification.

But the GPS_LocationChanged method never fires, so the location remains as ... and not the latitude/longitude I was hoping for.

some devices take their sweet time to start the app after boot
You are right there, but I have left this run for hours and the GPS coordinates never appear - even after ensuring clear view of the sky etc.

I will try a few other devices and see if it might just be an isolated case with my Motorola device.

Thank you both for your ideas - I will report soon.
 
Upvote 0

Ralph Parkhurst

Member
Licensed User
Longtime User
Here is an update that more accurately describes what I am seeing:

1. When the app runs in the foreground, or when the app runs in the background, or even when the app is force-quit by the user, the my location is regularly updated with a silent notification appearing like this (my lat/lon hidden for privacy, but yes I'm in Melbourne):


2. When the device is rebooted (Motorola G51 with Android 12), the following silent notification appears within about 30 seconds:


3. After 50 minutes had elapsed, the GPS coordinates were still not displayed. so the notification looks like this:


I am now thinking that the RunAtStart:True is running fine, but the GPS itself is not refreshing. Even after leaving it for several hours, the location still did not appear.

Three more observations:
- As soon as I tap the silent notification, the activity opens and the position is displayed within a few seconds
- The code I am using is the example as provided - no changes
- I am doing all this in an area with very good GPS satellite coverage

Does this sound like an issue with this particular device? If so I could grab another device/brand.
Does this sound like an issue with my Android version? If so I could get an Android 13 device.
Is there any workaround I could try to ensure that the GPS starts at boot?

My B4A version is 12.80
My manifest is:
Manifest:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="33"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.DarkTheme)
'End of default text.
SetServiceAttribute(Tracker, android:foregroundServiceType, "location")
 
Upvote 0

Ralph Parkhurst

Member
Licensed User
Longtime User
Worth debugging it with this library: https://www.b4x.com/android/forum/threads/gnss-library-an-updated-gps-library.109110/#content

It provides more information on the satellites reception.
You can also try to switch to FusedLocationProvider.
Thank you Erel. I will try both of those suggestions. I already have Graham's GNSS lib used in another project, so it should be ultra easy to change.

It's been a while since I used FusedLocationProvider, but I will try and see.

Warm regards,
Ralph
 
Upvote 0

jtare

Active Member
Licensed User
Longtime User
Hi @Ralph Parkhurst, were you able to figure this out? I'm facing a similar issue with a foreground tracking service, in my case I do not start tracking at boot but rather after the Recognition Detection Receiver fires, but I get a similar behavior like you mention, I get the notification but it seems like the tracking service gets killed after a few short seconds so the notification never gets updated with GPS data.
 
Upvote 0

Ralph Parkhurst

Member
Licensed User
Longtime User
Hi @jtare - I tried the GNSS library instead of the GPS lib, however unfortunately the results were the same.

Because of other work priorities, I have not yet tried the FusedLocationProvider as Erel suggested - that's still on my list to try - but it might take me a few weeks to get to it!!

Best of luck, and if/when I make progress I will report back here.

Kind regards,
Ralph
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…