RESOLUTION_REQUIRED即使用户的位置已开启。有可能吗?

时间:2018-03-02 17:17:44

标签: android location

我在应用上使用Google的SettingsClient。它工作正常,但有些用户无法使用GPS,即使他们已打开位置并且他们已经授予了对位置权限的访问权限。根据我的理解,如果Settings ClientFailureListener上返回 RESOLUTION_REQUIRED ,那是因为用户已禁用位置。

这是真的吗?或者是否有其他原因 RESOLUTION_REQUIRED 被退回?

当返回 RESOLUTION_REQUIRED 时,状态栏上不会显示GPS图标,但它应该在那里!

这是我的代码:

    SettingsClient mSettingsClient =
            LocationServices.getSettingsClient(context);

    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(UPDATE_INTERVAL_MS);
    mLocationRequest.setFastestInterval(FASTEST_INTERVAL_MS);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    LocationSettingsRequest.Builder builder = new 
    LocationSettingsRequest.Builder();
    builder.addLocationRequest(mLocationRequest);
    mLocationSettingsRequest = builder.build();

    mSettingsClient.checkLocationSettings(mLocationSettingsRequest)
            .addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() {
                @Override
                public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
                    connected = true;
                    // Fused Location code...
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    connected = false;
                    gpsFailureMessage = e.getMessage();
                }
            });

2 个答案:

答案 0 :(得分:1)

我正在解决此问题,并通过定位模式找出了此问题,即使GPS开启了,但是如果定位模式仍然选择“仅GPS”,则会发生此异常,当我更改为另一种模式时,它可以正常工作。 不幸的是,您无法通过编程方式设置位置模式,但可以将用户直接发送到设置屏幕,在该屏幕上他可以执行此操作,请遵循此提示https://stackoverflow.com/a/25745124/8921450

GPS mode picture

答案 1 :(得分:0)

以编程方式解决此问题的一种方法是,按照this官方android文档中所述检查可用位置的准确性。我这样做,在您的locationUtils类中像这样创建一个伴随对象(静态函数)

class LocationUtils {

    //other methods

   companion object {

    fun  checkLocationRequest(context: FragmentActivity, successCase : () -> Unit) {
        val builder = LocationSettingsRequest.Builder()
            .addLocationRequest(createLocationRequest())

        val client: SettingsClient = LocationServices.getSettingsClient(context)
        val task = client.checkLocationSettings(
            builder.build()
        )

        task.addOnSuccessListener {
            successCase()
        }

        task.addOnFailureListener { exception ->
            if(exception is ResolvableApiException) {
                try {
                    exception.startResolutionForResult(context, CHECK_LOCATION_SETTING)
                } catch (sendEx: IntentSender.SendIntentException) {

                }
            }
        }
    }

然后您可以像这样调用函数

LocationUtils.checkLocationRequest(activity){
    runHappyCase() //perform operation you intend to do with the location
}

如果位置不是处于高精度状态,则会显示一个对话框,提示用户选择高精度。您可以通过onActivityResult()

查看用户的回复