Forum Xamarin.Forms

Check the Settings.Secure.LOCATION_MODE in Xamarin Android

n 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

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    Could you please post a basic demo so that we can test with it ?

Sign In or Register to comment.