asmgx
asmgx

Reputation: 8044

Displaying Allow app to access device's location

I am new to android programming and I need to display an alert so that user give permission to my app automatically when he/she clicks on the allow button.

Like this app

enter image description here

but actually my code does a message that will just take the user to the setting so he/she can change it manually, and not everyone know how to do it manually

enter image description here

how can I get the first alert so that the app gain permission automatically instead of my current way where it just move the user to the setting page and they have to do it manually

here is my code

  public void showSettingsAlert() {
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(_activity);
        alertDialog.setTitle("GPS Settings");
        alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu ?");

        alertDialog.setPositiveButton("Settings",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(
                                Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                        _activity.startActivity(intent);
                    }
                });

        alertDialog.setNegativeButton("Cancel",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });

        alertDialog.show();
    }

Upvotes: 2

Views: 4278

Answers (1)

Elias N
Elias N

Reputation: 1450

Allowing your app access to the device's location (first screenshot) is not the same as enabling GPS (second screenshot). You actually need both: first check if you have permission as other answers have suggested, then check if GPS is enabled as you are already doing.

If you want to enable the GPS programmatically, you need to use the "Settings API". Check this answer on how to implement it.

Upvotes: 2

Related Questions