简体   繁体   English

华为定位工具包 - requestLocationUpdates 不会停止,即使 removeLocationUpdates 被调用

[英]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.我有一个问题,GPS 位置图标总是在通知栏中,即使我有这个 function 来停止请求位置更新。

private fun stopLocationUpdates() {
    fusedLocationProviderClientClient.removeLocationUpdates(locationCallback)
}

I call the function inside onPause()我在 onPause() 中调用 function

override fun onPause() {
    super.onPause()
    stopLocationUpdates()
}

Now removeLocationUpdates(locationCallback) works when I popBackStack from my current fragment.现在,当我从当前片段中弹出BackStack 时, removeLocationUpdates(locationCallback) 可以工作。 The gps location icon disappears from the notification. gps 位置图标从通知中消失。 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.但是,当我从当前片段导航到另一个片段时,即使从 onPause 执行 removeLocationUpdates(locationCallback),通知上的 gps 位置图标也不会 go 离开。 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()我在 onMapReady() 中调用了 requestLocationUpdates

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()
    }
}

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.您在顶部状态栏上显示的 GPS 图标的问题可以在我使用华为 HMS 定位套件和 Map 套件开发的演示应用程序上重现。 Looking at your code posted, you have been using both HMS Location and Map kit.查看您发布的代码,您一直在使用 HMS Location 和 Map 套件。 The key point to solve the issue is to remove the current location call in both location and map kit integration.解决该问题的关键是在位置和 map 套件集成中删除当前位置调用。

  1. Remove the HMS Location Kit location update by calling the function removeLocationUpdates(locationCallback) .通过调用 function removeLocationUpdates(locationCallback)删除 HMS Location Kit 位置更新。 You and other answers posted have already done.您和其他发布的答案已经完成。

  2. Remove the HMS Map kit getting current location by calling:移除 HMS Map 套件,通过调用获取当前位置:

    hMap.setMyLocationEnabled(false); hMap.setMyLocationEnabled(false);

This is what your code is missing in order to remove the GPS icon on the top status bar.这是为了删除顶部状态栏上的 GPS 图标而缺少的代码。

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.顺便说一句,这个解决方案在我使用华为 Mate 30 Pro 的开发计算机上得到了验证。

It is recommended that you call removeLocationUpdates() in onDestroy state when destroying as well.建议您在销毁时也调用 onDestroy state 中的removeLocationUpdates()

  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.您还可以参考此链接了解活动的 state 路径。

Hope this could help with your issue.希望这可以帮助您解决问题。

Do you want to let the gps location icon on the notifications go away?您想让通知 go 上的 gps 位置图标消失吗?

After run the stopLocationUpdates(), is the new location callback received, and let you notifications update?运行 stopLocationUpdates() 后,是否收到新的位置回调,并让您更新通知? Did you use NotificationManager to show notifications?您是否使用 NotificationManager 显示通知?

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());
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 华为 Map 和 Location Kit - 在 LocationCallback 中调用 getMapAsync() 时出现错误 - Huawei Map and Location Kit - Bug when getMapAsync() is called inside LocationCallback 即使参数0,requestLocationUpdates()如何获取位置? - How does requestLocationUpdates() get location even though has paramter 0? 华为 Map 套件 - 在 getMapAsync() 之后未调用 onMapReady() - Huawei Map Kit - onMapReady() not called after getMapAsync() 删除位置 huawei kit android studio 时出错 - Error removing location huawei kit android studio 华为定位套件地理围栏抛出错误 10201 - Huawei Location Kit Geofence is throwing Error 10201 位置管理器的requestLocationUpdates只调用一次 - Location Manager's requestLocationUpdates called only once 华为地图套件 - 在 Xamarin.Android 中的 GetMapAsync() 之后未调用 OnMapReady() - Huawei Map Kit - OnMapReady() not called after GetMapAsync() in Xamarin.Android 如果调用requestLocationUpdates以获取用户的新位置,则应用程序崩溃 - Application crashed if requestLocationUpdates is called to get new location of user 网络位置提供程序不适用于ICS(华为) - Network location provider does not work on ICS(Huawei ) 华为定位包在室内无法获取到我的位置 - Huawei Location Kit Fails to Obtain My Location when I am Indoor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM