为什么LocationSettingsResult startResolutionForResult没有调用onActivityResult?

时间:2015-12-18 18:33:40

标签: android google-play-services

我已经看过这个Q& A LocationSettingsRequest dialog - onActivityResult() skipped了。这不是同一个问题,因为一切都在活动中完成。

所使用的代码几乎是逐字逐字显示在Google Play服务示例中。

我有一个活动LocationActivity,它连接到GoogleApiClient以获取用户的位置。连接后,我会创建LocationSettingsRequest以确保打开位置设置。该活动正在实施ResultCallback<LocationSettingsResult>

调用

ResultCallback<LocationSettingsResult>.onResult(),如果调用result.getStatus().getStatusCode() == LocationSettingsStatusCodes.RESOLUTION_REQUIRED,则调用status.startResolutionForResult(this, REQUEST_CHECK_SETTINGS)并显示对话框。 问题是,无论选择什么,都不会调用onActivityResult()

@Override
public void onConnected(Bundle connectionHint) {
    Log.i(TAG, "GoogleApiClient connected");
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
        .addLocationRequest(new LocationRequest().setPriority(LocationRequest.PRIORITY_LOW_POWER));

    PendingResult<LocationSettingsResult> result =
        LocationServices.SettingsApi.checkLocationSettings(googleApiClient, builder.build());

    result.setResultCallback(this);
}

@Override
public void onResult(LocationSettingsResult result) {
    final Status status = result.getStatus();
    Log.d(TAG, "onResult() called with: " + "result = [" + status.getStatusMessage() + "]");
    switch (status.getStatusCode()) {
        case LocationSettingsStatusCodes.SUCCESS:
            getLocation();
            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(this, REQUEST_CHECK_SETTINGS);
            } catch (IntentSender.SendIntentException e) {
                Log.d(TAG, "", e);
                // Ignore the error.
            }
            break;
        case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
            showManualInputDialog();
            break;
    }
}

我从未到过这里:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d(TAG, "onActivityResult() called with: " + "requestCode = [" + requestCode + "], resultCode = [" + resultCode + "], data = [" + data + "]");
    switch (requestCode) {
        case REQUEST_CODE_RESOLUTION:
            retryConnecting();
            break;
        case REQUEST_CHECK_SETTINGS:
            if (resultCode == Activity.RESULT_OK) {
                getLocation();
            } else {
                showManualInputDialog();
            }
            break;
        default:
            super.onActivityResult(requestCode, resultCode, data);
            break;
    }
}

暂且不说。它在我的S3上工作了几次。从我可以说它停止工作时,我选择永远不再问。但是,它还没有在模拟器或Tab 10上工作,它不再适用于我的S3。

4 个答案:

答案 0 :(得分:7)

我觉得很傻。我的活动在清单中有noHistory="true",所以当其他活动开始时,没有任何东西可以回复。

答案 1 :(得分:4)

如果您正在Fragment中运行此代码,请不要使用startResolutionForResult()。改为使用 startIntentSenderForResult(status.getResolution().getIntentSender(), REQUEST_CODE_LOCATION_SETTING, null, 0, 0, 0, null);

和片段中的重写器onaActivityResult()。 结果将仅传递给该方法。

答案 2 :(得分:0)

在我的情况下出现了这个错误: 我用过

public abstract class AGoogleDriveBase extends ABase implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {
//...
@Override
    protected void onActivityResult(int requestCode, int resultCode,  Intent data) {
//...

并且未使用

调用此函数
try {
            result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
        } catch (SendIntentException e) {
            Log.e(TAG, "Exception while starting resolution activity", e);
        }

因为当我使用主要活动时

public class myAct extends AGoogleDriveBase implements ... {
//...
@Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {

未调用的super方法(不存在此字符串):

 super.onActivityResult(requestCode, resultCode, data);

答案 3 :(得分:0)

如果有人偶然发现了这个问题,那么gianlucaparadise的答案就是解决这个问题的更好方法之一。尽管答案被删除,但它增加了很多价值。