android студия 3.6
фрагмент
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationCallback
import com.google.android.gms.location.LocationRequest
import com.google.android.gms.location.LocationResult
private lateinit var locationCallback: LocationCallback
private lateinit var locationRequest: LocationRequest
private lateinit var fusedLocationProviderClient: FusedLocationProviderClient
init {
Debug.d(TAG, "INIT:")
profile.value = ProfileService.getProfile()
generateBarCodeValue()
// init location update
locationRequest = LocationRequest()
locationRequest.setInterval(
getApplication<Application>().applicationContext.resources.getInteger(
R.integer.polling_period_mylocation_mills
).toLong()
)
fusedLocationProviderClient =
FusedLocationProviderClient(getApplication<Application>().applicationContext)
locationCallback = object : LocationCallback() {
// Call only if turn on geolocation
override fun onLocationResult(locationResult: LocationResult?) {
Debug.d(TAG, "onLocationResult: locationResult = $locationResult")
locationResult ?: return
for (location in locationResult.locations) {
Debug.d(TAG, "onLocationResult: location = $location")
val newNearestGazStationData: Pair<Float, GazStation>? =
GazStationService.getNearestGazStation(location)
if (newNearestGazStationData != null) {
nearestGazStationData.value = SingleEvent(newNearestGazStationData)
}
}
}
}
}
fun startLocationUpdates(isPermissionGranted: Boolean) {
if (isPermissionGranted) {
FusedLocationProviderClient(getApplication<Application>().applicationContext).requestLocationUpdates(
locationRequest,
locationCallback,
Looper.getMainLooper()
)
}
}
fun stopLocationUpdates() {
Debug.d(TAG, "stopLocationUpdates: ")
fusedLocationProviderClient.removeLocationUpdates(locationCallback)
}
Когда пользователь включает службу определения местоположения, я вызываю метод startLocationUpdates
. Установите интервал 5 минут. Но метод обратного вызова onLocationResult
не является вызовом.
Почему?