为什么LocationSettingsResult startResolutionForResult不能调用onActivityResult? [英] Why is LocationSettingsResult startResolutionForResult not calling onActivityResult?

查看:117
本文介绍了为什么LocationSettingsResult startResolutionForResult不能调用onActivityResult?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



所使用的代码几乎是逐字的,它是Google Play服务示例中给出的。 p>

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


$ b

ResultCallback< LocationSettingsResult> .onResult )被调用,如果 result.getStatus()。getStatusCode()== LocationSettingsStatusCodes.RESOLUTION_REQUIRED 然后 status.startResolutionForResult这个,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()with:+result = [+ status.getStatusMessage()+]);
switch(status.getStatusCode()){
case LocationSettingsStatusCodes.SUCCESS:
getLocation();
休息;
案例LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
//位置设置不满意。但可以通过显示用户
//一个对话框来修复。
try {
//通过调用startResolutionForResult(),
//显示对话框,并在onActivityResult()中检查结果。
status.startResolutionForResult(this,REQUEST_CHECK_SETTINGS);
} catch(IntentSender.SendIntentException e){
Log.d(TAG,,e);
//忽略错误。
}
break;
case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
showManualInputDialog();
休息;
}
}

我永远不会在这里:

  @Override 
protected void onActivityResult(int requestCode,int resultCode,Intent data){
Log.d(TAG,onActivityResult ()调用:+requestCode = [+ requestCode +],resultCode = [+ resultCode +],data = [+ data +]);
switch(requestCode){
case REQUEST_CODE_RESOLUTION:
retryConnecting();
休息;
case REQUEST_CHECK_SETTINGS:
if(resultCode == Activity.RESULT_OK){
getLocation();
} else {
showManualInputDialog();
}
break;
默认值:
super.onActivityResult(requestCode,resultCode,data);
休息;
}
}

它在我的S3上工作了几次。从我所能告诉它停止工作,当我选择不再问。但是,它从来没有在模拟器或Tab 10上工作,它不再适用于我的S3。

我感到很傻。我的活动在清单中有 noHistory =true,所以当另一个活动开始时,没有东西可以回来。


I've seen this Q&A LocationSettingsRequest dialog - onActivityResult() skipped. It isn't the same issue because everything is being done in an Activity already.

The code used is almost verbatim what is given in the Google Play Services examples.

I have an activity, LocationActivity, that connects to GoogleApiClient for getting the user's location. Once connected I create a LocationSettingsRequest to make sure that location settings are turned on. The activity is implementing ResultCallback<LocationSettingsResult>.

ResultCallback<LocationSettingsResult>.onResult() is called and if result.getStatus().getStatusCode() == LocationSettingsStatusCodes.RESOLUTION_REQUIRED then status.startResolutionForResult(this, REQUEST_CHECK_SETTINGS) is called and the dialog is shown. The problem, no matter what is selected, onActivityResult() is never called.

@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;
    }
}

I never get here:

@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;
    }
}

As an aside. It worked a few times on my S3. From what I can tell it stopped working when I chose to never ask again. But, it hasn't ever worked on an emulator or a Tab 10 and it no longer works on my S3.

解决方案

Well I feel silly. My Activity had noHistory="true" in the Manifest so when the other Activity was started there was nothing to come back to.

这篇关于为什么LocationSettingsResult startResolutionForResult不能调用onActivityResult?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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