我正在使用CheckLocationSettings来检查位置是否已启用。这在我的Galaxy S5上工作正常。但是,在模拟器android kitkat 4.4上,无论位置是启用还是禁用,LocationSettingsResult始终为6(ResolutionRequired)。 如果我用YES回答位置请求弹出窗口,则OnActivityResult始终为RESULT_CANCELED
我正在使用xamarin,所以有些部分看起来不同:
protected void buildGoogleApiClient()
{
mGoogleApiClient = new GoogleApiClient.Builder(this)
.AddConnectionCallbacks(this)
.AddOnConnectionFailedListener(this)
.AddApi(LocationServices.API)
.Build();
}
protected void createLocationRequest()
{
mLocationRequest = new LocationRequest();
mLocationRequest.SetInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.SetFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.SetPriority(LocationRequest.PriorityLowPower);
}
protected void buildLocationSettingsRequest()
{
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.AddLocationRequest(mLocationRequest);
builder.SetAlwaysShow(true);
mLocationSettingsRequest = builder.Build();
}
protected async Task CheckLocationSettings()
{
var result = await LocationServices.SettingsApi.CheckLocationSettingsAsync(
mGoogleApiClient, mLocationSettingsRequest);
await HandleResult(result);
}
public async Task HandleResult(LocationSettingsResult locationSettingsResult)
{
var status = locationSettingsResult.Status;
switch (status.StatusCode)
{
case CommonStatusCodes.Success:
await getlastknownlocation();
break;
case CommonStatusCodes.ResolutionRequired:
try
{
status.StartResolutionForResult(this, REQUEST_CHECK_SETTINGS);
}
catch (IntentSender.SendIntentException)
{
}
break;
case LocationSettingsStatusCodes.SettingsChangeUnavailable:
break;
}
}
protected override async void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
switch (requestCode)
{
case REQUEST_CHECK_SETTINGS:
switch (resultCode)
{
case Result.Ok:
await getlastknownlocation();
break;
case Result.Canceled:
break;
}
break;
}
}
THX
答案 0 :(得分:0)
如果您获得RESOLUTION_REQUIRED
(C#:ResolutionRequired
),则需要请求用户许可。
状态代码RESOLUTION_REQUIRED表示必须更改设置。要提示用户修改位置设置的权限,请调用startResolutionForResult(Activity,int)。此方法会显示一个对话框,要求用户修改位置设置。
使用您可以在StartResolutionForResult
中检查的结果代码调用OnActivityResult
,以确定用户是否授予了权限:
if (status.StatusCode == LocationSettingsStatusCodes.ResolutionRequired)
{
status.StartResolutionForResult(this, 9999);
}
参考:https://developer.android.com/training/location/change-location-settings.html