25

I am making GPS android app (with air for android) and what I want is if GPS is off I want to open GPS settings on device or switch GPS on but I don't know how. I want to do it in AS3 or open android settings in Java. Thanks for help!

Tinko
  • 502
  • 1
  • 4
  • 14

2 Answers2

57

You can simply start an activity with this action:

startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));

I use it inside a dialog:

public static void displayPromptForEnablingGPS(final Activity activity)
    {

        final AlertDialog.Builder builder =  new AlertDialog.Builder(activity);
        final String action = Settings.ACTION_LOCATION_SOURCE_SETTINGS;
        final String message = "Do you want open GPS setting?";

        builder.setMessage(message)
            .setPositiveButton("OK",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface d, int id) {
                        activity.startActivity(new Intent(action));
                        d.dismiss();
                    }
            })
            .setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface d, int id) {
                        d.cancel();
                    }
            });
        builder.create().show();
    }
GVillani82
  • 17,196
  • 30
  • 105
  • 172
  • 3
    but when I go to this activity `Settings.ACTION_LOCATION_SOURCE_SETTINGS` and enable gps and again come back to my application then my gps doesnt seems to be enabled for the application and when i refresh then again this alert is shown again to enable the gps – Samarth Kejriwal Aug 14 '17 at 09:47
  • Don't forget to check if (intent.resolveActivity(packageManager) != null) – Yvgen Mar 14 '18 at 13:03
  • @SamarthKejriwal The same thing happened to me, apparently a colleague found a patch / solution adding: alertDialog.dismiss(); alertDialog.cancel(); context.startActivity(Intent(action)) – Gerrard Dec 04 '20 at 03:40
10
final Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
Karthik
  • 2,282
  • 2
  • 22
  • 23