【发布时间】:2019-04-01 08:35:53
【问题描述】:
在我的 Xamarin 表单应用程序 (Android/IOS) 中,我正在使用 GPS 定位服务。我可以检查 GPS 是否打开/关闭状态,并据此打开设置。我的问题是我想检查定位模式。高精度/仅限设备。我想检查它,因为如果用户使用通知面板打开 GPS,它将启用仅设备模式。但我想检查该状态并告诉用户选择高精度模式。
按钮点击背后的代码。我在这里使用 DependencyService。
private async void Button_Clicked(object sender, EventArgs e)
{
var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location);
if (status == PermissionStatus.Granted)
{
DependencyService.Get<ISettingsService>().OpenSettings();
this.IsBusy = true;
this.IsEnabled = false;
if (CrossConnectivity.Current.IsConnected)
{
try
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(10));
Position positiony = new Position(position.Latitude, position.Longitude);
var geocoder = new Geocoder();
var addresses = await geocoder.GetAddressesForPositionAsync(positiony);
string cuAddress = addresses.ToList()[0];
lAddress.Text = cuAddress;
if (cuAddress.Count() > 0)
{
sendAccidentData(position, cuAddress, vNum, pNum);
}
else
{
Debug.WriteLine("No address!");
}
}
catch (OperationCanceledException coe)
{
Debug.WriteLine(coe.Message);
}
}
else
{
DependencyService.Get<IMessageService>().MessageService();
}
this.IsBusy = false;
this.IsEnabled = true;
}
else {
await DisplayAlert("Location", "Need location permission!", "OK");
}
}
这是打开位置设置的android代码。
class SettingsServiceAndroid : ISettingsService
{
public void OpenSettings()
{
LocationManager locationManager = (LocationManager)Android.App.Application.Context.GetSystemService(Context.LocationService);
if (locationManager.IsProviderEnabled(LocationManager.GpsProvider) == false)
{
Toast.MakeText(Android.App.Application.Context, "Please enable GPS!", ToastLength.Long).Show();
Intent gpsSettingIntent = new Intent(Settings.ActionLocationSourceSettings);
gpsSettingIntent.AddFlags(ActivityFlags.NewTask);
gpsSettingIntent.AddFlags(ActivityFlags.MultipleTask);
Android.App.Application.Context.StartActivity(gpsSettingIntent);
}
}
}
我试过了,但无法访问 getContentResolver()
Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
我想要这样的东西......
if(Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE) == 3){
//do somthing
}else{
//do somthing
}
3 表示高精度
【问题讨论】:
标签: c# xamarin xamarin.forms