我正在开发一个Xamarin下的跨平台移动应用程序。应用程序需要知道用户的坐标。我已经尝试使用谷歌的融合API,以便在Android情况下获得最好的准确性。在阅读了几十篇关于如何在安卓中处理这种情况的博客文章后,我写了一些代码来检查手机设置是否足以支持融合应用编程接口(启用位置并设置为高精度+ WiFi打开或WiFi扫描始终打开)。如果不满足这些前提条件,那么代码将在这种情况下启动默认的Google解析,并期望用户以OnActivityResult格式回复。尽管用户按下了"OK“,OnActivityResult中的代码仍然收到resultCode = Result.Canceled。但是,设置已更改!我还尝试在OnActivityResult中再次执行检查,结果是分辨率再次弹出,但这次Ok确实发送了resultCode = Result.Ok信号。我已经在stackoverflow中阅读了相当多的相关帖子,尝试了一些建议的技巧,但都没有解决问题。自11月初以来,我已经在Xamarin论坛上发布了这个问题,但还没有人提出解决方案。我正在中兴通讯刀片L3安卓5.0设备上测试这个应用程序。我在本文的末尾发布了Android活动的代码。如果有人能提点建议,我将不胜感激。非常感谢您的宝贵时间。
[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;
}
}
}https://stackoverflow.com/questions/41469166
复制相似问题