1

I'm using FusedLocationProvider in my app and I noticed that when my app is in the background and I start some other app that contains Google Map my original app starts receiving location updates extremely fast (like 1 update per second) despite setting up the fastest interval.

I know that I should unregister when going to background etc but this is not the case here.

Any ideas why this might happen or where I can report it to Google? This is the activity I start it from (I've removed couple of permissions check just for the visibility) The full repo can be found here

class MainActivity : AppCompatActivity() {

private val locationController by lazy { LocationController.getInstance(applicationContext) }

lateinit var button: Button
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    button = findViewById(R.id.button)

    button.setOnClickListener {
        if (locationController.isStarted) {
            locationController.stop()
            button.text = "START LOCATION UPDATES"
        } else {
            locationController.start()
            button.text = "STOP LOCATION UPDATED"
        }
    }    
}

And the LocationController looks like this:

class LocationController(context: Context) {

companion object {
    @Volatile private var INSTANCE: LocationController? = null

    fun getInstance(context: Context): LocationController {
        return INSTANCE ?: synchronized(this) {
            INSTANCE ?: LocationController(context).also { INSTANCE = it }
        }
    }
}

private val fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(context)
private val locationRequest by lazy {
    LocationRequest.create()
        .setInterval(INTERVAL_MILLIS)
        .setFastestInterval(FASTEST_INTERVAL_MILLIS)
        .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
}
private val locationCallback = object : LocationCallback() {
    override fun onLocationResult(locationResult: LocationResult) {
        super.onLocationResult(locationResult)
        Log.d("boom", "onLocationResult! ${locationResult.lastLocation}")
    }

    override fun onLocationAvailability(locationAvailability: LocationAvailability) {
        super.onLocationAvailability(locationAvailability)
    }
}
var isStarted: Boolean = false

@SuppressLint("MissingPermission")
fun start() {
    fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.getMainLooper())
        .addOnSuccessListener {
            Log.d("boom", "requestLocationUpdates success!")
        }
    isStarted = true
}

fun stop() {
    fusedLocationProviderClient.removeLocationUpdates(locationCallback)
        .addOnSuccessListener {
            Log.d("boom", "removeLocationUpdates success!")
        }
    isStarted = false
}

The constant values I experience it with are:

const val INTERVAL_MILLIS = 30_000L 
const val FASTEST_INTERVAL_MILLIS = 10_000L
Jogosb
  • 155
  • 1
  • 12
  • 1
    Does this answer your question? [Difference between LocationRequest setInterval (long millis) and LocationRequest setFastestInterval (long millis)](https://stackoverflow.com/questions/26114345/difference-between-locationrequest-setinterval-long-millis-and-locationrequest) – Justin Poehnelt Feb 10 '22 at 00:26
  • @jpohnelt - it does not, my problem is that although I set up setFastestInterval to 10-15 seconds I got the location updates every 1 second. – Jogosb Feb 10 '22 at 08:19
  • Thanks for the updates, looks much better now! – Ryan M Feb 10 '22 at 08:30
  • [docs](https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest#setFastestInterval(long)): "All location requests are considered hints, and you may receive locations that are more/less accurate, and faster/slower than requested." ... and ... "This controls the fastest rate at which your application will receive location updates, which might be faster than setInterval(long) in some situations (for example, if other applications are triggering location updates)." Use BG `requestLocationUpdates (LocationRequest request, PendingIntent callbackIntent)` – Computable Feb 10 '22 at 11:54
  • Yep, also "Unlike setInterval(long), this parameter is exact. Your application will never receive updates faster than this value." And I'm receiving updates 10 times faster than declared - I've also tried implementation with PendingIntent and the result is the same. – Jogosb Feb 10 '22 at 12:02

0 Answers0