Android FusedLocation的SettingsApi不能运行多次 [英] Android FusedLocation's SettingsApi does not work more than once

查看:196
本文介绍了Android FusedLocation的SettingsApi不能运行多次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FusedLocation有一个很棒的API来检查用户是否启用了所有相关的设置来获取位置信息。我遵循文档 https://developers.google.com / android / reference / com / google / android / gms / location / SettingsApi 来启动SettingsApi。我的代码来检查用户的设置如下

  private void checkLocationSetting()
{
if(mLocationSettingsRequest == null)
{
mLocationSettingsRequest =
LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest)
.setAlwaysShow(true)
.build( );
}

PendingResult< LocationSettingsResult> result =
LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient,mLocationSettingsRequest);

result.setResultCallback(new ResultCallback< LocationSettingsResult>()
{
@Override
public void onResult(LocationSettingsResult result)
{
final Status status = result.getStatus();
final LocationSettingsStates state = result.getLocationSettingsStates();
switch(status.getStatusCode())
{
case LocationSettingsStatusCodes.SUCCESS:
{
//所有位置设置都满足,客户端可以初始化位置
//在这里请求
requestNewLocationBySettingLocationReq();
break;
}
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
{
//位置设置不满意,但可以通过显示用户
来修复//一个对话框。
尝试
{
//通过调用startResolutionForResult(),
//显示对话框并在onActivityResult()中检查结果。
status.startResolutionForResult(SpotToiletMap.this,REQUEST_CHECK_SETTINGS);
} catch(IntentSender.SendIntentException e)
{
//忽略错误。
}
break;
}
案例LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
{
//位置设置不满足。但是,我们无法修复
//设置,所以我们不会显示对话框。
MiscellaneousMethods.showGenericSnackBar(rootView,getString(R.string.location_settings_unavailable));
休息;
}
}
}
});
}

这段代码第一次工作得很好。随后,PendingResult CallBack从未工作。我无法在logcat上找到任何日志。对我来说,它看起来像一个实施问题。



以下是重现该问题的方法。



关闭您的GPS。尝试调用该方法,并按预期完美工作。它要求我切换n个GPS。一旦您收到位置更新,请再次关闭GPS并调用此方法。我从来没有得到一个对话框来启用GPS。我试图获取用户的最新位置,调用此方法。



编辑:
我尝试将PendingResult结果声明为类变量。仍然没有运气。我也尝试通过检查结果是否为空来初始化结果,如预期的那样,正如Google的文档中提到的那样,它会抛出一个异常提及结果已经被使用了。

解决方案

看起来问题在于,您不会每次重新创建 LocationSettingsRequest.Builder ,因为您只需重新创建它如果它是空的。



我得到它的工作,在Android 4.4.4和6.0.1测试。



每次按下按钮时都会显示对话框(前提是按钮按下时设备上的位置已禁用):

  public void checkLocationSetting(){
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(30 * 1000);
mLocationRequest.setFastestInterval(5 * 1000);

LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
.addLocationRequest(mLocationRequest);
builder.setAlwaysShow(true);

result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient,builder.build());

result.setResultCallback(new ResultCallback< LocationSettingsResult>(){
@Override
public void onResult(LocationSettingsResult result){
final Status status = result.getStatus );
switch(status.getStatusCode()){
case LocationSettingsStatusCodes.SUCCESS:
//所有位置设置都满足,客户端可以在这里初始化位置
//请求。
// ...
break;
case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
//位置设置不满足,但可以通过显示用户
//一个对话框
try {
//通过调用startResolutionForResult(),
来显示对话框并检查onActivityResult()中的结果
status.startResolutionForResult(
MainActivity.this,
REQUEST_LOCATION);
} catch(IntentSender.SendIntentException e){
//忽略错误。
}
break;
案例LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
//位置设置不满意。但是,我们无法修复
//设置,所以我们不会显示对话框。
// ...
break;
}
}
});

}


FusedLocation has a wonderful API to check if the user has enabled all the relevant settings to obtain location. I followed the documentation https://developers.google.com/android/reference/com/google/android/gms/location/SettingsApi to fire up the SettingsApi. My code to check user's setting is as below

private void checkLocationSetting()
{
    if (mLocationSettingsRequest == null)
    {
        mLocationSettingsRequest =
                new LocationSettingsRequest.Builder()
                        .addLocationRequest(mLocationRequest)
                        .setAlwaysShow(true)
                        .build();
    }

    PendingResult<LocationSettingsResult> result =
            LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, mLocationSettingsRequest);

    result.setResultCallback(new ResultCallback<LocationSettingsResult>()
    {
        @Override
        public void onResult(LocationSettingsResult result)
        {
            final Status status = result.getStatus();
            final LocationSettingsStates state = result.getLocationSettingsStates();
            switch (status.getStatusCode())
            {
                case LocationSettingsStatusCodes.SUCCESS:
                {
                    // All location settings are satisfied. The client can initialize location
                    // requests here.
                    requestNewLocationBySettingLocationReq();
                    break;
                }
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                {
                    // Location settings are not satisfied. But could be fixed by showing the user
                    // a dialog.
                    try
                    {
                        // Show the dialog by calling startResolutionForResult(),
                        // and check the result in onActivityResult().
                        status.startResolutionForResult(SpotToiletMap.this, REQUEST_CHECK_SETTINGS);
                    } catch (IntentSender.SendIntentException e)
                    {
                        // Ignore the error.
                    }
                    break;
                }
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                {
                    // Location settings are not satisfied. However, we have no way to fix the
                    // settings so we won't show the dialog.
                    MiscellaneousMethods.showGenericSnackBar(rootView, getString(R.string.location_settings_unavailable));
                    break;
                }
            }
        }
    });
}

This piece of code works perfectly fine for the first time. Subsequently, the PendingResult CallBack never worked. I was not able to find any logs on logcat as well. For me it looks like an implementation issue.

Here is how to reproduce the issue.

Turn off your GPS. Try to call the method and it perfectly works as expected. It asks me to switch n GPS. Once you receive location update, turn off the GPS again and call this method. I never get a dialog to enable GPS. I call this method on a button click which tries to get the latest location of the user.

EDIT: I tried to declare PendingResult result as a class variable. Still no luck. I also tried to initialize result only once by checking if it is null, as expected and as mentioned in Google's documentation it throws an exception mentioning the result is already consumed.

解决方案

It looks like the problem is that you're not re-creating the LocationSettingsRequest.Builder each time, as you only re-create it if it's null.

I got it working, tested on both Android 4.4.4 and 6.0.1.

The dialog is shown each time the button is pressed (provided that Location was disabled on the device between button presses):

public void checkLocationSetting() {
    mLocationRequest = LocationRequest.create();
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setInterval(30 * 1000);
    mLocationRequest.setFastestInterval(5 * 1000);

    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(mLocationRequest);
    builder.setAlwaysShow(true);

    result = LocationServices.SettingsApi.checkLocationSettings(mGoogleApiClient, builder.build());

    result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
        @Override
        public void onResult(LocationSettingsResult result) {
            final Status status = result.getStatus();
            switch (status.getStatusCode()) {
                case LocationSettingsStatusCodes.SUCCESS:
                    // All location settings are satisfied. The client can initialize location
                    // requests here.
                    //...
                    break;
                case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                    // Location settings are not satisfied. But could be fixed by showing the user
                    // a dialog.
                    try {
                        // Show the dialog by calling startResolutionForResult(),
                        // and check the result in onActivityResult().
                        status.startResolutionForResult(
                                MainActivity.this,
                                REQUEST_LOCATION);
                    } catch (IntentSender.SendIntentException e) {
                        // Ignore the error.
                    }
                    break;
                case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
                    // Location settings are not satisfied. However, we have no way to fix the
                    // settings so we won't show the dialog.
                    //...
                    break;
            }
        }
    });

}

这篇关于Android FusedLocation的SettingsApi不能运行多次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆