1 year ago

#157814

test-img

Jogosb

FusedLocationProvider rapid updates when Google Maps are turned on

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

android

google-maps

google-play-services

android-location

fusedlocationproviderapi

0 Answers

Your Answer

Accepted video resources