Android StartResolutionForResult始终以OnActivityResult结束,resultCode=Result.Cancelled
我正在Xamarin下开发一个跨平台的移动应用程序。应用程序需要知道用户的坐标。我已经尝试使用谷歌的融合API,以在Android的情况下获得尽可能高的准确性。在阅读了几十篇关于如何在Android中处理此类情况的博客文章后,我编写了代码,检查手机设置是否足以启用融合API定位,并设置为高精度+WiFi打开或WiFi扫描始终打开。如果不满足这些先决条件,那么代码将启动此案例的默认Google解析,并期望用户在OnActivityResult中给出回复。尽管作为用户按了OK,OnActivityResult中的代码仍会继续接收resultCode=Result.Cancelled。但是,设置已更改!!!我还尝试在OnActivityResult中再次执行检查,结果是分辨率第二次弹出,但这次Ok确实发送了一个resultCode=result.Ok信号。我已经在stackoverflow中阅读了不少相关文章,尝试了一些建议的技巧,但都没有解决这个问题。自11月初以来,我已经在Xamarin论坛上发布了这个问题,但还没有人提出解决方案。我正在中兴Blade L3安卓5.0设备上测试该应用程序。我在本文末尾发布了Android活动的代码。如果有人能提出一些建议,我将不胜感激。非常感谢您抽出时间Android StartResolutionForResult始终以OnActivityResult结束,resultCode=Result.Cancelled,android,xamarin,Android,Xamarin,我正在Xamarin下开发一个跨平台的移动应用程序。应用程序需要知道用户的坐标。我已经尝试使用谷歌的融合API,以在Android的情况下获得尽可能高的准确性。在阅读了几十篇关于如何在Android中处理此类情况的博客文章后,我编写了代码,检查手机设置是否足以启用融合API定位,并设置为高精度+WiFi打开或WiFi扫描始终打开。如果不满足这些先决条件,那么代码将启动此案例的默认Google解析,并期望用户在OnActivityResult中给出回复。尽管作为用户按了OK,OnActivityR
[Activity(Label = "TestLocation",
Icon = "@drawable/icon",
Theme = "@style/MainTheme",
MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity,
GoogleApiClient.IConnectionCallbacks,
GoogleApiClient.IOnConnectionFailedListener,
Android.Gms.Location.ILocationListener
{
private GoogleApiClient locationClient;
private LocationRequest locationRequest;
private LocationSettingsRequest locationSettingsRequest;
private const int MIN_TIME_BW_UPDATES = 1000 * 3;
private const int REQUEST_CHECK_SETTINGS = 9000;
public async void OnConnected(Bundle connectionHint)
{
await CheckFusedApiSettings();
}
public void OnConnectionFailed(ConnectionResult result)
{
throw new NotImplementedException();
}
public void OnConnectionSuspended(int cause)
{
throw new NotImplementedException();
}
public void OnLocationChanged(Location location)
{
throw new NotImplementedException();
}
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
InitiateClient();
InitiateRequests();
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
protected override void OnStart()
{
base.OnStart();
locationClient.Connect();
}
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CHECK_SETTINGS)
{
switch (resultCode)
{
case Result.Canceled:
Toast.MakeText(this, "RESULT CANCEL", ToastLength.Short).Show();
break;
case Result.Ok:
Toast.MakeText(this, "RESULT OK", ToastLength.Short).Show();
break;
case Result.FirstUser:
default:
break;
}
}
}
private void InitiateClient()
{
locationClient = new GoogleApiClient.Builder(this, this, this)
.AddApi(LocationServices.API)
.Build();
locationClient.Connect();
}
private void InitiateRequests()
{
locationRequest = new LocationRequest();
locationRequest.SetPriority(LocationRequest.PriorityHighAccuracy);
locationRequest.SetFastestInterval(MIN_TIME_BW_UPDATES / 2);
locationRequest.SetInterval(MIN_TIME_BW_UPDATES);
LocationSettingsRequest.Builder settingsBuilder = new LocationSettingsRequest.Builder();
settingsBuilder.AddLocationRequest(locationRequest);
locationSettingsRequest = settingsBuilder.Build();
}
private async Task CheckFusedApiSettings()
{
var locationSettingsResult = await LocationServices.SettingsApi.CheckLocationSettingsAsync(locationClient, locationSettingsRequest);
switch (locationSettingsResult.Status.StatusCode)
{
case LocationSettingsStatusCodes.Success:
Toast.MakeText(this, "SUCCESS", ToastLength.Short).Show();
break;
case LocationSettingsStatusCodes.ResolutionRequired:
try
{
locationSettingsResult.Status.StartResolutionForResult(this, REQUEST_CHECK_SETTINGS);
}
catch (Exception e)
{
Toast.MakeText(this, "CANCEL: " + e.Message, ToastLength.Short).Show();
}
break;
default:
locationClient.Disconnect();
break;
}
}
}
如对上述问题感兴趣,请访问: 在那里,我发布了一个经过7-8个月的努力后得出的结论