首页 > 解决方案 > addOnFailureListener() 如果用户曾经启用并在一段时间后禁用位置,则不会调用。使用 LocationServices.getSettingsClient() 时

问题描述

  1. 我曾经LocationServices.getSettingsClient()删除 depriated
    LocationServices.SettingsApi.checkLocationSettings()
  2. 如果位置被禁用,它会在 mainActivity 启动时要求用户第一次启用位置。
  3. 如果在两者之间我禁用位置。它不是要求启用位置。
    result.addOnSuccessListener即使我的位置被禁用,也会调用手段。
  4. 如果位置在中间被禁用,它应该询问 result.addOnFailureListener
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(getContext());
        mSettingsClient = LocationServices.getSettingsClient(getContext());
        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
        builder.addLocationRequest(mLocationRequest);
        mLocationSettingsRequest = builder.build();
        mLocationService = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
        Task<LocationSettingsResponse> result =
                LocationServices.getSettingsClient(getContext()).checkLocationSettings(builder.build());
  result.addOnSuccessListener(getActivity(), new OnSuccessListener<LocationSettingsResponse>() {
            @Override
            public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
               
              LocationSettingsResponse response =
                            task.getResult(ApiException.class);

                    LocationSettingsStates locationSettingsStates = response.getLocationSettingsStates();
                    if (!locationSettingsStates.isGpsPresent() || !locationSettingsStates.isGpsUsable()) {
          //check point 4 
            }
        });

        result.addOnFailureListener(getActivity(), new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                if (e instanceof ResolvableApiException) {
                    // Location settings are not satisfied, but this can be fixed
                    // by showing the user a dialog.
                    try {
                        // Show the dialog by calling startResolutionForResult(),
                        // and check the result in onActivityResult().
                        ResolvableApiException resolvable = (ResolvableApiException) e;
                        resolvable.startResolutionForResult(getActivity(),
                                REQUEST_CHECK_SETTINGS);
                    } catch (IntentSender.SendIntentException sendEx) {
                        // Ignore the error.
                    }
                }
            }
        }); 

  1. 在这里,我需要使用以下代码打开位置启用对话框:
final Status status = result.getStatus();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
                    Log.i(TAG, "All location settings are satisfied.");
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    Log.i(TAG, "Location settings are not satisfied. Show the user a dialog to upgrade location settings ");

                    try {
                        // Show the dialog by calling startResolutionForResult(), and check the result
                        // in onActivityResult().
                        status.startResolutionForResult(MainActivity.this, REQUEST_CHECK_SETTINGS);
                    } catch (IntentSender.SendIntentException e) {
                        Log.i(TAG, "PendingIntent unable to execute request.");
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog not created.");
                    break;
            }

5 .但是使用give 不会给出我在 catch 块中收到的SettingsClient onSucessstatusCode Task<LocationSettingsResponse> result

更新

第一个标签是尝试OnComplete()位置默认情况下的一部分,第二次我关闭位置并转到其他Fragment并再次来到主要片段。即使我的位置被禁用,Tt 也执行了尝试部分

在此处输入图像描述

标签: javaandroidgoogle-mapslocationfusedlocationproviderclient

解决方案


推荐阅读