【发布时间】:2017-05-19 01:25:54
【问题描述】:
我正在 Xamarin 下开发一个跨平台的移动应用程序。应用需要知道用户的坐标。我尝试使用 Google 的 Fused API 在 Android 案例中尽可能准确。在阅读了几十篇关于如何在 Android 中处理这种情况的博客文章后,我编写了代码来检查手机设置是否足以支持 Fused API(启用位置并设置为高精度 + WiFi 打开或 WiFi 扫描始终打开) .如果不满足这些先决条件,则代码会针对这种情况启动默认的 Google 解析,并期望用户在 OnActivityResult 中回复。尽管作为用户按“确定”,但 OnActivityResult 中的代码仍会继续接收 resultCode = Result.Canceled。但是,设置已更改!我也尝试在 OnActivityResult 中再次执行检查,结果是第二次弹出分辨率,但这次 Ok 确实发送了一个 resultCode = Result.Ok 信号。我已经在 stackoverflow 中阅读了相当多的相关帖子,尝试了一些建议的技巧,但都没有解决问题。自 11 月初以来,我已经在 Xamarin 论坛上发布了该问题,但尚未有人提出解决方案。我正在 ZTE Blade L3 Android 5.0 设备上测试该应用程序。我在最后发布了 Android Activity 的代码。如果有人可以提出建议,我将不胜感激。非常感谢您的宝贵时间。
[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;
}
}
}
【问题讨论】: