I'm using Huawei Location Kit. I have a problem where the GPS location icon is always in the notification bar even I have this function to stop requesting location updates.
private fun stopLocationUpdates() { fusedLocationProviderClientClient.removeLocationUpdates(locationCallback) }
I call the function inside onPause()
override fun onPause() { super.onPause() stopLocationUpdates() }
Now removeLocationUpdates(locationCallback) works when I popBackStack from my current fragment. The gps location icon disappears from the notification. But when I navigate from my current fragment to another fragment, the gps location icon on the notifications does not go away even if removeLocationUpdates(locationCallback) was executed from onPause. The only way to stop the request is by closing the app in the recently used apps.
Does anyone know the cause?
Here are some of my other code:
private var fusedLocationProviderClientClient: FusedLocationProviderClient? = null private var locationRequest: LocationRequest? = null private var locationCallback: LocationCallback? = null private var huaweiMap: HuaweiMap? = null private fun prepareLocation() { locationRequest ?: LocationRequest.create() .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY) .setInterval(30000) .setFastestInterval(10000) .setSmallestDisplacement(250.toFloat()) .also { locationRequest = it } locationCallback ?: object : LocationCallback() { override fun onLocationResult( locationResult: LocationResult? ) { locationResult ?: return locationResult.lastLocation ?: return currentLocation = LatLng( locationResult.lastLocation.latitude, locationResult.lastLocation.longitude ) } override fun onLocationAvailability( locationAvailability: LocationAvailability? ) { } }.also { locationCallback = it } fusedLocationProviderClientClient ?: LocationServices.getFusedLocationProviderClient(requireActivity()) .also { fusedLocationProviderClientClient = it } } private fun requestLocationUpdates() { locationClient?.requestLocationUpdates(locationRequest, locationCallback, null) }
I called requestLocationUpdates inside onMapReady()
override fun onMapReady(hMap: HuaweiMap?) { // if maps is not yet initialized if (huaweiMap == null ) { huaweiMap = hMap prepareLocationRequest() checkPermissionThenRequestLocation() } else { //insert code } } private fun checkPermissionThenRequestLocation() { if (!(isFineLocationGranted && isCoarseLocationGranted)) { val permissions = arrayOf( Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION ) requestPermissions( permissions, MainActivity.PERMISSION_REQ_CODE_LOCATION ) } else { requestLocationUpdates() } }
https://stackoverflow.com/questions/65840834/huawei-location-kit-requestlocationupdates-does-not-stop-even-removelocationup January 22, 2021 at 03:18PM
没有评论:
发表评论