0

I'm trying to create a foreground service that can track the user's location with the phone's GPS. I have a ForegroundService class, where in the onCreate() method I call a function that should start by asking for location updates:

fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)

locationManager = getSystemService(LOCATION_SERVICE) as LocationManager

val locationListener: LocationListener = this
locationManager.requestLocationUpdates(
    LocationManager.GPS_PROVIDER, 2000, 0F, locationListener
)

My class is implementing Service() and LocationListener. I then override onLocationChanged, where I should log the current speed (and leave onAccuracyChanged empty).

Of course I have a notification correctly showing in android, so the service is definitely running, but I seem to get no location updates anyways. What could the problem be? Thank you.

Enrico Cortinovis
  • 811
  • 3
  • 8
  • 31
  • Did you request runtime permissions for location? I don't see it there, so I'm assuming not. You also need to request static permission in the manifest. – Gabe Sechan Oct 28 '21 at 05:09
  • @GabeSechan Yes, and it is checked that they have been accepted before requesting the updates. The code I’m trying to use for the foreground service was working in a normal activity, with screen on etc. – Enrico Cortinovis Oct 28 '21 at 05:39
  • Did the user grant background location gathering permission? In modern Android, a user can choose to allow location to be gathered when the app is foreground only. Even if your app is foreground, running it in a Service counts as background. – Gabe Sechan Oct 28 '21 at 13:34
  • @GabeSechan Yes, that's done too. – Enrico Cortinovis Oct 28 '21 at 16:31
  • does the same code you present work when ran in an Activity (in foreground)? – Jakub Licznerski Nov 01 '21 at 19:23
  • @JakubLicznerski This is with [this](https://stackoverflow.com/questions/69748480/use-onsensorchanged-updates-inside-a-foreground-service) question. In both cases the code that was normally working in an Activity doesn't work in a foreground service. – Enrico Cortinovis Nov 01 '21 at 19:26

1 Answers1

1

You haven't shared much code so I'll share a few guesses as an answer proposal:

  1. Location API selection:

You are creating a FusedLocationProviderClient, but not using it. According to this docs and this SO post it is preferred to use this provider over LocationManager because of accuracy and battery usage. Try using requestLocationUpdates from FusedLocationProviderClient. Additional example here.


  1. Listener definition:

According to this piece of code:

locationManager.requestLocationUpdates(
    LocationManager.GPS_PROVIDER, 2000, 0F, locationListener
)

you are trying to use this overload of this method which then uses a Looper of a thread in which it is defined. If the listener is not created within the service it may use a Looper which is not accessible from the service's thread.


Ensure you are calling startForeground(id, notification) in onStartCommand and that the service is staying alive (example).

Also check out this repo for an example of location services accessed within a foreground service. Read through this post as well for additional reference.

Jakub Licznerski
  • 1,008
  • 1
  • 17
  • 33