Search code examples
androidxamarinxamarin.formsgps

How to programmatically turn on GPS on Android


I have survey some article on stackoverflow.

It seems android version above 4.0 is not able to turn on/off GPS programmatically.

Is there anything changed now? Or is this still not possible?


Solution

  • You can't turn it on/off programatically due to security reasons. According to Change location settings docs:

    If your app needs to request location or receive permission updates, the device needs to enable the appropriate system settings, such as GPS or Wi-Fi scanning. Rather than directly enabling services such as the device's GPS, your app specifies the required level of accuracy/power consumption and desired update interval, and the device automatically makes the appropriate changes to system settings.

    So, the best thing to do is, if the user has disabled the GPS, then for better UX you can show a prompt and redirect the user to the Location activity like this:

    MainActivity.Instance.StartActivity(new Intent(ActionLocationSourceSettings));
    

    Where MainActivity.Instance is your current Activity.