You can open with

startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);

You can return by pressing back button on device.

Answer from kjurkovic on Stack Overflow
🌐
Medium
droidbyme.medium.com › android-turn-on-gps-programmatically-d585cf29c1ef
Android Turn on GPS programmatically | by Droid By Me | Medium
January 2, 2019 - Programmatically we can turn on GPS in two ways. First, redirect the user to location settings of a device (by code) or another way is to ask to turn on GPS by GPS dialog using LocationSettingsRequest and SettingsClient.
Top answer
1 of 8
131

Use below code to check. If it is disabled, dialog box will be generated

public void statusCheck() {
    final LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
        buildAlertMessageNoGps();

    }
}

private void buildAlertMessageNoGps() {
    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(final DialogInterface dialog, final int id) {
                    startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(final DialogInterface dialog, final int id) {
                    dialog.cancel();
                }
            });
    final AlertDialog alert = builder.create();
    alert.show();
}
2 of 8
19

Here is a simple way of programmatically enabling location like Maps app:

protected void enableLocationSettings() {
       LocationRequest locationRequest = LocationRequest.create()
             .setInterval(LOCATION_UPDATE_INTERVAL)
             .setFastestInterval(LOCATION_UPDATE_FASTEST_INTERVAL)
             .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

        LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                .addLocationRequest(locationRequest);

        LocationServices
                .getSettingsClient(this)
                .checkLocationSettings(builder.build())
                .addOnSuccessListener(this, (LocationSettingsResponse response) -> {
                    // startUpdatingLocation(...);
                })
                .addOnFailureListener(this, ex -> {
                    if (ex instanceof ResolvableApiException) {
                        // Location settings are NOT satisfied,  but this can be fixed  by showing the user a dialog.
                        try {
                            // Show the dialog by calling startResolutionForResult(),  and check the result in onActivityResult().
                            ResolvableApiException resolvable = (ResolvableApiException) ex;
                            resolvable.startResolutionForResult(TrackingListActivity.this, REQUEST_CODE_CHECK_SETTINGS);
                        } catch (IntentSender.SendIntentException sendEx) {
                            // Ignore the error.
                        }
                    }
                });
 }

And onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if (REQUEST_CODE_CHECK_SETTINGS == requestCode) {
        if(Activity.RESULT_OK == resultCode){
            //user clicked OK, you can startUpdatingLocation(...);

        }else{
            //user clicked cancel: informUserImportanceOfLocationAndPresentRequestAgain();
        }
    }
}

You can see the documentation here: https://developer.android.com/training/location/change-location-settings

🌐
DeveloperMemos
developermemos.com › posts › opening-android-device-settings-kotlin
Opening Android Device Settings Programmatically with Kotlin | DeveloperMemos
Learn how to open device settings programmatically in Android using Kotlin, allowing users to navigate directly to specific settings screens from your app.
🌐
Our Code World
ourcodeworld.com › articles › read › 318 › how-to-open-android-settings-programmatically-with-java
How to open Android Settings programmatically with Java | Our Code World
December 5, 2016 - To display the Settings page programmatically, you can use the startActivityForResult method with an Intent object and a constant of the Settings, the following example should open the general settings menu of Android:
🌐
Jim Wilson
jwhh.com › 2013 › 03 › 20 › programmatically-enable-android-location-services
Programmatically Enable Android GPS and Location Services – Jim Wilson (a.k.a. hedgehogjim)
March 20, 2013 - Pre-Android 6.0 devices still represent nearly 50% of Android devices currently in use .. and on those devices … we have to take the user to the settings screen. ... Well it’s hard to say without knowing the specific error message. One thing that comes to mind is the class imports … Did you import the Settings class (android.provider.Settings)?
🌐
Android Developers
developer.android.com › core areas › sensors and location › change location settings
Change location settings | Sensors and location | Android Developers
July 7, 2023 - In order to use the location services ... location provider, connect your app using the Settings Client, then check the current location settings and prompt the user to enable the required settings if needed....
🌐
Stack Overflow
stackoverflow.com › questions › 20894983 › how-to-open-settings-more-location-services-my-places-programatically
android - How to open Settings > More > Location Services > My Places programatically - Stack Overflow
final Intent intent = new Intent(Intent.ACTION_MAIN, null); intent.addCategory(Intent.CATEGORY_LAUNCHER); final ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.bluetoothSettings"); intent.setComponent(cn); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); ... Enabling Location mode High Accuracy or Battery saving, programmatically, without user needing to visit Settings
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › how-to-enable-disable-the-gps-programmatically-in-android
How to enable/disable the GPS programmatically in Android?
August 2, 2019 - import android.content.Context; import android.content.Intent; import android.location.LocationManager; import android.provider.Settings; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { Button button; Context context; Intent intent1; TextView textview; LocationManager locationManager ; boolean GpsStatus ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.a
🌐
Medium
i-amvariable.medium.com › turning-on-gps-in-your-device-without-going-to-settings-12d1049225a3
Turning on GPS in your Device without going to Settings | by iamVariable | Medium
June 4, 2019 - We can turn on GPS programmatically in two ways. redirecting the user to location settings of a device (by code) another way is to ask to turn on GPS by GPS dialog using LocationSettingsRequest and SettingsClient.
🌐
codestudy
codestudy.net › blog › opening-android-settings-programmatically
How to Open Android Settings Programmatically: A Complete Developer's Guide — codestudy.net
Explain Why: Before sending users to settings, explain why they need to go there (e.g., "Enable location to use map features"). Minimize Disruption: Only direct users to settings when absolutely necessary (avoid overusing this flow). Test Across Versions: Some intents behave differently on older Android versions (e.g., permission settings pre-API 30). Use Consistent UI: Match your app’s navigation style (e.g., use a button labeled "Open Settings" instead of "Go Here").
🌐
GeeksforGeeks
geeksforgeeks.org › open-specific-settings-using-android-application
Open Specific Settings Using Android Application | GeeksforGeeks
November 5, 2020 - Many times while the user is using an android application he has to edit a few settings in the device to use the applications such as providing any permissions from the settings application to use that specific feature within the application. For changing these settings we have to open a specific se ... There are some necessary building blocks that an Android application consists of.
🌐
YouTube
youtube.com › watch
GPS popup - Turn on GPS/location without going to the settings in Android | Kotlin - YouTube
This video explains how to check device_location/GPS and show a popup/dialogue if it is disabled/OFF. If user clicks OK button in the popup, the location wil...
Published   September 10, 2022
🌐
Stack Overflow
stackoverflow.com › questions › 63243321 › android-11-how-to-open-system-location-permission-settings-screen-programmatica
Android-11: How to open system location permission settings screen programmatically? - Stack Overflow
By this way, I have to open "Settings" manually. How can I use an Intent to start the activity of location permission settings for my App programmatically inside my app? ... so it's duplicate, please delete it, Android 30 API allows automatically open it if ACCESS_FINE_LOCATION was granted and then you request again location permission but this time background stackoverflow.com/a/63487691/4548520
🌐
Luasoftware
code.luasoftware.com › tutorials › android › android-open-app-permission-screen-programmatically
Android Open App Permission Screen Programmatically
November 22, 2019 - Technically, you cannot open App Permission screens directly, but you can open App Settings screen which can access Permission.
Top answer
1 of 3
3

The solution here is not to try to use an intent to launch the location settings in Android. The way Android works on Android API >= 30, is that you first need to request location permissions for coarse and fine location. After the user has allowed the permission, then make another location permission request, for background location. So:

Step 1:

val permissionsToRequest: Array<String> = arrayOf(
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION
            )
            launcher.launch(permissionsToRequest)

Step 2 (assuming user allows coarse/fine location permission in step 1):

val permissionsToRequest: String =
                Manifest.permission.ACCESS_BACKGROUND_LOCATION
            launcher.launch(permissionsToRequest)

However, instead of a dialog coming up for the "Allow all the time" location permission, Android navigates user to the location settings for the Android app.

2 of 3
2

Use this method so that a pop will automatic come for Allow All The Time..

private void getPermission()
{

    progressBar.setVisibility(View.VISIBLE);
    ActivityResultLauncher<String[]> locationPermissionRequest =
            registerForActivityResult(new ActivityResultContracts
                            .RequestMultiplePermissions(), result -> {
                        Boolean fineLocationGranted = result.getOrDefault(
                                Manifest.permission.ACCESS_FINE_LOCATION, false);
                        Boolean coarseLocationGranted = result.getOrDefault(
                                Manifest.permission.ACCESS_COARSE_LOCATION, false);
                        if (fineLocationGranted != null && fineLocationGranted) {
                            // Call Your Method
                        } else if (coarseLocationGranted != null && coarseLocationGranted) {
                            // Only approximate location access granted.
                            // Call Your Method
                        } else {
                            // No location access granted.
                        }
                    }
            );

    locationPermissionRequest.launch(new String[] {
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION
    });
}