'How to open location settings in android using xamarin

I try to open location settings in android using this tutorial, but in this method:

public void OpenSettings()
    {
        
    }

I don't know what to write to openned location settings.

Do I need additional libraries ?

I write this code:

public interface ILocSettings
    {
        void OpenSettings();

    }

    public void OpenSettings()
    {
        
    }

In the GPS button method I write this code:

 var myAction = await DisplayAlert("Location", "Please Turn On Location", "OK", "CANCEL");
                if (myAction)
                {
                    if (Device.RuntimePlatform == global::Xamarin.Forms.Device.Android)
                    {

                        //DependencyService.Get<ISettingsService>().OpenSettings();
                        global::Xamarin.Forms.DependencyService.Get<ILocSettings>().OpenSettings();
                    }
                    else
                    {
                        _ = DisplayAlert("Device", "You are using some other shit", "YEP");
                    }
                }
                else
                {
                    _ = DisplayAlert("Alert", "User Denied Permission", "OK");
                }

So the message appear, but I don't know what to write in the OpenSettings() to open GPS location settings on the phone..



Solution 1:[1]

Opening other Activities on Android is done through Intents. To open Location Settings you can use the ACTION_LOCATION_SOURCE_SETTINGS intent.

Something like this would probably work:

var intent = new Android.Content.Intent(Android.Provider.Settings.ActionLocationSourceSettings);
Android.App.Application.Context.StartActivity(intent);

If Android doesn't like the Application Context, you might need to resolve the top Activity and use that as your Context instead.

If you are using Xamarin.Essentials you can get the top activity with: Platform.CurrentActivity:

var intent = new Android.Content.Intent(Android.Provider.Settings.ActionLocationSourceSettings);
Platform.CurrentActivity.StartActivity(intent);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cheesebaron