我正在Xamarin下开发一个跨平台的移动应用程序。该应用需要知道用户的坐标。我尝试使用Google的Fused API,以便在Android案例中获得最佳准确性。在阅读了几篇关于如何在Android中处理此类案例的博客文章后,我编写了一些代码,用于检查手机设置是否足以支持Fused API(启用位置并设置为高精度+ WiFi打开或WiFi扫描始终上)。如果不满足这些先决条件,则代码会针对此案例启动默认的Google解决方案,并期望用户在OnActivityResult中的回复。尽管紧迫" OK"作为用户,OnActivityResult中的代码保持接收resultCode = Result.Canceled。但是,设置已更改!!!我还尝试在OnActivityResult中再次执行检查,结果是分辨率弹出第二次,但这次Ok确实发送了resultCode = Result.Ok信号。我已经在stackoverflow中阅读了不少相关帖子,尝试了一些建议的技巧,但都没有解决问题。自11月初以来,我已经在Xamarin论坛上发布了这个问题,但还没有人提出解决方案。我正在测试中兴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;
}
}
}
答案 0 :(得分:-1)
如果您对上述问题感兴趣,请访问: https://forums.xamarin.com/discussion/comment/275171#Comment_275171 在经过7-8个月的努力之后,我已经得出了一个结论。