我已经看到了这个Q&A LocationSettingsRequest对话框 - 跳过了onActivityResult().这不是同一个问题,因为一切都在Activity中完成.
使用的代码几乎是逐字逐句的,在Google Play服务示例中给出了.
我有一个活动,LocationActivity
它连接到GoogleApiClient以获取用户的位置.连接后,我创建一个LocationSettingsRequest
以确保打开位置设置.活动正在实施ResultCallback
.
ResultCallback
被称为如果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)); PendingResultresult = 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.
好吧,我觉得很傻.我的活动noHistory="true"
在Manifest中,所以当其他Activity开始时,没有什么可以回来的.
如果您在Fragment中运行此代码,则不要使用startResolutionForResult()。改为使用
startIntentSenderForResult(status.getResolution().getIntentSender(), REQUEST_CODE_LOCATION_SETTING, null, 0, 0, 0, null);
和片段中的重写器onaActivityResult()。结果将仅传递给该方法。