Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 views
in Technique[技术] by (71.8m points)

android - Huawei Location Kit - requestLocationUpdates does not stop even removeLocationUpdates was called

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
        prepareLocation()
        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()
    }
}
question from:https://stackoverflow.com/questions/65840834/huawei-location-kit-requestlocationupdates-does-not-stop-even-removelocationup

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your issue of the GPS icon displaying on the top status bar could be reproduced on my demo app that I developed using the Huawei HMS Location Kit and Map Kit. Looking at your code posted, you have been using both HMS Location and Map kit. The key point to solve the issue is to remove the current location call in both location and map kit integration.

  1. Remove the HMS Location Kit location update by calling the function removeLocationUpdates(locationCallback). You and other answers posted have already done.

  2. Remove the HMS Map kit getting current location by calling:

    hMap.setMyLocationEnabled(false);

This is what your code is missing in order to remove the GPS icon on the top status bar.

Please let us know if the above suggestion solve your issue. BTW, this solution is verified on my development computer with Huawei Mate 30 Pro.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...