java - 如何正确停止FusedLocationProviderClient?

标签 java android fusedlocationproviderclient

我在 Service 中使用 FusedLocationProviderClient
我想以正确的方式“阻止”它。

使用下面的代码好吗?

 @Override
    public void onDestroy() {
        super.onDestroy();
        // Stop Looper of FusedLocationProviderClient  
        if (locationClient != null) {
            locationClient = null;
        }
    }

其余代码

FusedLocationProviderClient locationClient;


protected void startLocationUpdates() {


        // Create the location request to start receiving updates
        mLocationRequest = new LocationRequest();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(UPDATE_INTERVAL);
        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);

        // Create LocationSettingsRequest object using location request
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
        builder.addLocationRequest(mLocationRequest);
        LocationSettingsRequest locationSettingsRequest = builder.build();

        // Check whether location settings are satisfied
        // https://developers.google.com/android/reference/com/google/android/gms/location/SettingsClient
        SettingsClient settingsClient = LocationServices.getSettingsClient(this);
        settingsClient.checkLocationSettings(locationSettingsRequest);

        // new Google API SDK v11 uses getFusedLocationProviderClient(this)
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            return;
        }

        locationClient = getFusedLocationProviderClient(this);getFusedLocationProviderClient(this).requestLocationUpdates(mLocationRequest, new LocationCallback() {
                    @Override
                    public void onLocationResult(LocationResult locationResult) {
                        // do work here
                        onLocationChanged(locationResult.getLastLocation());
                    }
                }, Looper.myLooper());
    }

最佳答案

只需调用 removeLocationUpdates在 onDestroy 中

对于requestLocationUpdates,它说:

This call will keep the Google Play services connection active, so make sure to call removeLocationUpdates(LocationCallback) when you no longer need it, otherwise you lose the benefits of the automatic connection management.

关于java - 如何正确停止FusedLocationProviderClient?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47864434/

相关文章:

android - 找不到 gradle signingconfigs

java - 避免方法调用 getLatitude 可能会产生 java.Lang.NullPointerException

java - 当我第二次运行接受权限请求对话框时,为什么我的应用程序不显示位置按钮?

java - RxJava - 在循环中合并请求序列

android - 通过 onListItemClick() 更改 Activity 内的 Fragments

java - java中的 "main"可以返回一个String吗?

android - libgdx:当我使用 Shaperenderer 添加 Actor 时,为什么我的所有 Actor 都没有渲染?

java - Spring Boot 应用程序 + Jolokia - 启动期间出现异常

java - Android 应用程序无法将详细信息发布到 url