c# - Check the Settings.Secure.LOCATION_MODE in Xamarin Android
问题描述
In my Xamarin form app (Android/IOS) I am using GPS location service. I can check whether GPS on/off status and according to that I can open the settings. My problem is I wanna check the location mode. High accuracy/Device only. I wanna check it because, if the user uses the Notification Panel to turn on the GPS it will enable the Device only mode. But I wanna check that status and tell the user to select the high accuracy mode.
Code behind button click. I use DependencyService here.
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");
}
}
This is the android code that open the location settings.
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);
}
}
}
I tried with this, but unable to access to getContentResolver()
Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);
I want something like this...
if(Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE) == 3){
//do somthing
}else{
//do somthing
}
3 for high accuracy
解决方案
我发现您的代码是 Java,因此您无法访问 getContentResolver()。
请使用以下代码修改您的代码:
locationMode = Settings.Secure.GetInt(ContentResolver, Settings.Secure.LocationMode);
推荐阅读
- javascript - 画布火花线
- c# - 调用 DeserializeObject<> 时抛出 Newtonsoft.Json.JsonReaderException
- ios - Swift:导入框架但在工作区中找不到类
- python - 为什么 linspace 和 interp1d 的输出总是一样的?
- python - matplotlib 标签格式化字体已更改
- android - 怎么不使用 findViewById?
- docker - Docker 运行不显示任何输出
- php - Laravel 共享主机,index.php 不指向 root 而是停留在 public_htmls
- wordpress - 使用自定义参数为 wordpress 创建 youtube 嵌入代码
- ios - 快速滚动视图中的意外视图