How to open GPS settings an android device?

Tinko picture Tinko · Apr 13, 2014 · Viewed 26.9k times · Source

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!

Answer

GVillani82 picture GVillani82 · Apr 13, 2014

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();
    }