2

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()
    }
}
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
ccd
  • 119
  • 6

3 Answers3

1

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.

Zinna
  • 1,947
  • 2
  • 5
  • 20
  • Thank you #2 is the solution to my question! I inserted the code inside onPause and now when I navigate to another fragment, location updates on the background now stops. – ccd Jan 27 '21 at 06:00
0

It is recommended that you call removeLocationUpdates() in onDestroy state when destroying as well.

  override fun onDestroy() {
        //Removed when the location update is no longer required.
        stopLocationUpdates()
        super.onDestroy()
    }

You also can refer this link for the state paths of an Activity.

Hope this could help with your issue.

zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
  • I tried this but onDestroy does not get called when navigating from current fragment to another fragment. Thank you for the help though – ccd Jan 22 '21 at 08:29
  • Have you try ```stopLocationUpdates() ```on onStop ? and you can check the log for ```removeLocationUpdatesWithCallback onSuccess\onFailure``` to see whether ```removeLocationUpdates()```is executed – zhangxaochen Jan 22 '21 at 08:58
  • I also tried calling stopLocationUpdates() inside onStop but its still the same. I also tried adding success and failed Listener and addOnSuccessListener is the one that gets printed on the logcat – ccd Jan 22 '21 at 09:11
  • fusedLocationProviderClientClient?.removeLocationUpdates(locationCallback)?. addOnSuccessListener { Log.d("Test", "remove success") }?.addOnFailureListener { Log.d("Test", "remove failed") } – ccd Jan 22 '21 at 09:12
  • have you got ```remove failed```log message? – zhangxaochen Jan 22 '21 at 09:15
  • Nope. **remove success** is the one that gets printed on the logcat – ccd Jan 22 '21 at 09:25
  • could you pls provide a screenshoot of GPS location icon in your notification bar ?and what is your phone model? – zhangxaochen Jan 25 '21 at 02:55
  • https://imgur.com/a/0HJiCPC My phone is a non-huawei device. The location icon stays there even calling stopLocationUpdates() when navigating to another fragment. So I assume the requestLocationUpdates is still running in the background – ccd Jan 25 '21 at 03:35
  • This log still displays in the logcat `I/HmsMapKit_HuaweiMapImpl_4: onLocationResult success` even when removeLocationUpdates(locationCallback) was called – ccd Jan 25 '21 at 09:11
0

Do you want to let the gps location icon on the notifications go away?

After run the stopLocationUpdates(), is the new location callback received, and let you notifications update ? Did you use NotificationManager to show notifications?

If there is no new location callback and after that, you can dismiss the notifications directly.

boolean isLocationSettingOk = false;

@Override
public void onPause() {
    super.onPause();
    removeLocationUpdatesWithCallback();
}
@Override
protected void onRestart() {
    super.onRestart();
    addLoationCallback();
}
@Override
public void onMapReady(HuaweiMap huaweiMap) {
    hMap = huaweiMap;
    hMap.setMyLocationEnabled(true);
    hMap.getUiSettings().setMyLocationButtonEnabled(true);
    initHwLoction();
}
public void initHwLoction() {
    fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
    mLocationRequest = new LocationRequest();
    SettingsClient settingsClient = LocationServices.getSettingsClient(this);
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
    builder.addLocationRequest(mLocationRequest);
    LocationSettingsRequest locationSettingsRequest = builder.build();
    settingsClient.checkLocationSettings(locationSettingsRequest)
            .addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() {
                @Override
                public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
                    isLocationSettingOk = true;
                    addLoationCallback();
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(Exception e) {
                    e.printStackTrace();
                }
            });
}

private void addLoationCallback() {
    if (isLocationSettingOk)
        fusedLocationProviderClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.getMainLooper())
                .addOnSuccessListener(new OnSuccessListener<Void>() {
                    @Override
                    public void onSuccess(Void aVoid) {
                        //接口调用成功的处理
                        Log.d(TAG, "onSuccess: " + aVoid);
                    }
                });
}
public void removeLocationUpdatesWithCallback() {
    try {
        Task<Void> voidTask = fusedLocationProviderClient.removeLocationUpdates(mLocationCallback);
        voidTask.addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                Log.i(TAG, "removeLocationUpdatesWithCallback onSuccess");
            }
        })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(Exception e) {
                        Log.e(TAG, "onFailure:" + e.getMessage());
                    }
                });
    } catch (Exception e) {
        Log.e(TAG, "exception:" + e.getMessage());
    }
}
Matrix
  • 503
  • 3
  • 17
  • After stopLocationUpdates(), location request callback is still being received in the background. Here is the logcat `I/HmsMapKit_HuaweiMapImpl_4: onLocationResult success`. No I don't use NotifcationManager. I assume the location gps icon is still showing in the notifcations because location updates is running in the background – ccd Jan 25 '21 at 09:07
  • I trid it in java, with both map kit and location kit of hms. Can‘t reproduce your question. So i post my code. Its writed in java, but I think its almost in same. – Matrix Jan 26 '21 at 03:08