1

Please help if anybody has idea what happened with this case.

I have BroadcastReceiver which register to ForegroundService. This BroadcastReceiver will listen to Intent.ACTION_SCREEN_ON and called startLocationUpdates() if it receive any.

fun startLocationUpdates(context: Context, entity: LockEntityDB?) {
    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(context)
    mSettingsClient = LocationServices.getSettingsClient(context)
    mLocationCallback = object : LocationCallback() {
        override fun onLocationResult(locationResult: LocationResult?) {
            super.onLocationResult(locationResult)

            val location = locationResult?.lastLocation
            locationResult?.let {
                saveLocation(context, it.lastLocation)
                locationUpdatesCount(context)
            }
            //something will do with entityDb
        }
    }


    mOneTimeLocationRequest = LocationRequest().apply {
        interval = 5 * 1000
        fastestInterval = 1000
        priority = LocationRequest.PRIORITY_HIGH_ACCURACY
        numUpdates = 1
    }

    val builder = LocationSettingsRequest.Builder()
    builder.addLocationRequest(mOneTimeLocationRequest!!)
    mLocationSettingsRequest = builder.build()

    mSettingsClient
            ?.checkLocationSettings(mLocationSettingsRequest)
            ?.addOnSuccessListener {
                mFusedLocationClient?.requestLocationUpdates(mOneTimeLocationRequest, mLocationCallback, null)
            }
            ?.addOnFailureListener { e ->
                kTimberLog(context, "loc " + e.toString())
            }
}

So the idea if user turn on the phone, it will send Intent.ACTION_SCREEN_ON and FusedLocationProviderClient will update actual user location.

The problem is, when user try to turn on the phone after 5-6x FusedLocationProviderClient never give any locationResult anymore.

The more weirdest part all this locationResult for the 6th times and so on will start reappearing when user open the MainActivity.

Additional info : I m testing this code on 3 emulator : Nexus 5 API 23, PIXEL 3 API 29, Galaxy Nexus API 29. It works fine for Nexus 5 API 23.

Anybody know what is the reason of this behavior? Thank you very much

1 Answers1

1

After struggling for few days, I found the solution here : https://developer.android.com/training/location/request-updates#continue-user-initiated-action

After adding this line into my manifest, everything looks working fine

<service
android:name="MyBackgroundService"
android:foregroundServiceType="location" ... >
...

I hope can help somebody who are stuck in same problem