SettingsClient.checkSettings 总是失败,状态为 LocationSettingsStatusCodes.RESOLUTION_REQUIRED
Posted
技术标签:
【中文标题】SettingsClient.checkSettings 总是失败,状态为 LocationSettingsStatusCodes.RESOLUTION_REQUIRED【英文标题】:SettingsClient.checkSettings always fails with status LocationSettingsStatusCodes.RESOLUTION_REQUIRED 【发布时间】:2020-02-06 09:39:09 【问题描述】:我在我的应用程序中使用LocationServices
。在使用位置服务之前,我试图验证具有所需设置的位置是否已打开,但我面临的问题是SettingsClient.checkSettings
总是failing
。请看我的LocationRequest
和LocationSettingRequest
builder:
位置请求
mLocationRequest = LocationRequest()
mLocationRequest.interval = interval
mLocationRequest.fastestInterval = interval
Log.v(TAG, "distance => $distance")
if(distance > 0)
mLocationRequest.smallestDisplacement = distance
mLocationRequest.priority = LocationRequest.PRIORITY_HIGH_ACCURACY
buildLocationSettingsRequest()
LocationSettingRequestBuilder
private fun buildLocationSettingsRequest()
val builder = LocationSettingsRequest.Builder()
builder.addLocationRequest(mLocationRequest)
mLocationSettingsRequest = builder.build()
mSettingsClientInit = true
我请求checkSettings
as
mSettingsClient.checkLocationSettings(mLocationSettingsRequest)
.addOnSuccessListener(this, object : OnSuccessListener<LocationSettingsResponse>
override fun onSuccess(locationSettingsResponse: LocationSettingsResponse)
Log.i(TAG, "LocationManager: All location settings are satisfied.");
mLocationCallback?.let
fusedLocationClient?.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
// updateUI();
)
.addOnFailureListener(this, object : OnFailureListener
override fun onFailure(e: Exception)
Log.v(TAG, "Request PErmission failure");
var statusCode = (e as ApiException).getStatusCode()
when (statusCode)
LocationSettingsStatusCodes.RESOLUTION_REQUIRED ->
try
// Show the dialog by calling startResolutionForResult(), and check the
// result in onActivityResult().
var rae = e as ResolvableApiException;
rae.startResolutionForResult(this@BaseLocationActivity, REQUEST_CHECK_SETTINGS);
catch (sie: IntentSender.SendIntentException)
Log.v(TAG, "PendingIntent unable to execute request.");
LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE ->
mRequestingLocationUpdates = false;
Log.v(TAG, "Settings change unavailable.");
)
它总是产生onFailure
的addOnFailureListener
。谁能帮我解决问题?此设置对话框显示确定以启用设置,我按确定,再次出现带有 onFailure 回调的相同对话框。
它发生在某些设备上,而不是所有设备上。
【问题讨论】:
您是否尝试了与PRIORITY_HIGH_ACCURACY
(see) 不同的优先级?
不,即使设备处于高精度模式,即 Enable providers are Network and GPS ,这个 checkLocationSettings 仍然返回失败
@Mustansar Saeed 参考 dis ``` ***.com/a/10311891/12273964 ``
【参考方案1】:
您好,请按照此代码解决您的问题,我添加了 GPSUtils 类,借助该类您可以轻松管理 GPS。
GpsUtils.Class
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.IntentSender;
import android.location.LocationManager;
import android.support.annotation.NonNull;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.common.api.ResolvableApiException;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationSettingsRequest;
import com.google.android.gms.location.LocationSettingsResponse;
import com.google.android.gms.location.LocationSettingsStatusCodes;
import com.google.android.gms.location.SettingsClient;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import static android.content.ContentValues.TAG;
public class GpsUtils
private Context context;
private SettingsClient mSettingsClient;
private LocationSettingsRequest mLocationSettingsRequest;
private LocationManager locationManager;
private LocationRequest locationRequest;
public GpsUtils(Context context)
this.context = context;
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
mSettingsClient = LocationServices.getSettingsClient(context);
locationRequest = LocationRequest.create();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(10 * 1000);
locationRequest.setFastestInterval(2 * 1000);
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(locationRequest);
mLocationSettingsRequest = builder.build();
//**************************
builder.setAlwaysShow(true); //this is the key ingredient
//**************************
// method for turn on GPS
public void turnGPSOn(onGpsListener onGpsListener)
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
if (onGpsListener != null)
onGpsListener.gpsStatus(true);
else
mSettingsClient
.checkLocationSettings(mLocationSettingsRequest)
.addOnSuccessListener((Activity) context, new OnSuccessListener<LocationSettingsResponse>()
@SuppressLint("MissingPermission")
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse)
// GPS is already enable, callback GPS status through listener
if (onGpsListener != null)
onGpsListener.gpsStatus(true);
)
.addOnFailureListener((Activity) context, new OnFailureListener()
@Override
public void onFailure(@NonNull Exception e)
int statusCode = ((ApiException) e).getStatusCode();
switch (statusCode)
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
try
// Show the dialog by calling startResolutionForResult(), and check the
// result in onActivityResult().
ResolvableApiException rae = (ResolvableApiException) e;
rae.startResolutionForResult((Activity) context, AppConstants.GPS_REQUEST);
catch (IntentSender.SendIntentException sie)
Log.i(TAG, "PendingIntent unable to execute request.");
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
String errorMessage = "Location settings are inadequate, and cannot be " +
"fixed here. Fix in Settings.";
Log.e(TAG, errorMessage);
Toast.makeText((Activity) context, errorMessage, Toast.LENGTH_LONG).show();
);
public interface onGpsListener
void gpsStatus(boolean isGPSEnable);
GPS 对话框将显示 SettingsClient 的失败回调,并导致活动的 onActivityResult()。所以基本上如果用户同意打开 GPS,我们可以在我们的活动中启用 GPS 标志。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK)
if (requestCode == AppConstants.GPS_REQUEST)
isGPS = true; // flag maintain before get location
现在在获取位置之前调用 GpsUtils 类。
new GpsUtils(this).turnGPSOn(new GpsUtils.onGpsListener()
@Override
public void gpsStatus(boolean isGPSEnable)
// turn on GPS
isGPS = isGPSEnable;
);
【讨论】:
问题是即使在某些设备中仍然满足成功侦听器的所有条件,也会发生 onError 并且并非总是如此,【参考方案2】:经过大量研究,我找到了解决方案...
问题基本上是如果LocationSettingsStatusCodes.RESOLUTION_REQUIRED == true
那么您进行的下一个连续调用将始终导致相同的输出
即,如果您递归调用此函数,您将始终递归地获得 RESOLUTION_REQUIRED,因此请避免递归调用此函数
你可能会如何递归调用它?
您可能会得到解析结果的 result_code,如果它是 RESULT_OK
,您可能会调用相同的函数来设置位置
欲了解更多信息,请在此处查看我对另一个类似问题的回答Android LocationServices.checkLocationSettings false negative result
【讨论】:
以上是关于SettingsClient.checkSettings 总是失败,状态为 LocationSettingsStatusCodes.RESOLUTION_REQUIRED的主要内容,如果未能解决你的问题,请参考以下文章