当前位置:  开发笔记 > 编程语言 > 正文

为什么LocationSettingsResult startResolutionForResult没有调用onActivityResult?

如何解决《为什么LocationSettingsResultstartResolutionForResult没有调用onActivityResult?》经验,为你挑选了2个好方法。

我已经看到了这个Q&A LocationSettingsRequest对话框 - 跳过了onActivityResult().这不是同一个问题,因为一切都在Activity中完成.

使用的代码几乎是逐字逐句的,在Google Play服务示例中给出了.

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

ResultCallback.onResult()被称为如果result.getStatus().getStatusCode() == LocationSettingsStatusCodes.RESOLUTION_REQUIREDstatus.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 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.



1> Jeff Engebre..:

好吧,我觉得很傻.我的活动noHistory="true"在Manifest中,所以当其他Activity开始时,没有什么可以回来的.



2> 小智..:

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

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

推荐阅读
wurtjq
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有