How to launch the location settings in Android for a specific app
Asked Answered
Y

3

3

To enable "Allow all the time" location permissions in an Android app, how to launch the location settings for the app in the Android settings?

I have seen how to launch the Android settings page for location (all apps), and how to launch the settings for a specific app, but not how to deep link to the location settings for a specific app.

This is what I currently have:

fun openAppLocationSettings(context: Context) {
        val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
        val packageName = context.packageName
        val uri = Uri.fromParts("package", packageName, null)
        intent.data = uri
        context.startActivity(intent)
    }

but it only shows the root page of the app Settings.

Yen answered 14/5, 2023 at 6:41 Comment(0)
Y
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.

Yen answered 19/5, 2023 at 7:2 Comment(1)
As soon as the user declined background location access, nothing happens if you request Manifest.permission.ACCESS_BACKGROUND_LOCATION. Just nothing visually. At least on Android 14, Pixel 6. However a permission declined callback is called.Roentgen
P
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
    });
}
Pattani answered 14/5, 2023 at 7:3 Comment(2)
Thanks- this is the code we already have, but the "Allow all the time" does not show. So I am trying to launch the Android settings via intent. Please see edit of question.Yen
Use this for open location setting. private void openLocationSettings() { Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(intent); } Sometimes, Allow all the time will not show depends on android device and sdkPattani
O
2

You can use the following code to open location settings in the Android settings:

Intent locSettings = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(locSettings);

To access the location privacy settings under security settings, you can use the following code:

Intent locSettings = new Intent(android.provider.Settings.ACTION_SECURITY_SETTINGS);
startActivityForResult(locSettings);

To do it in Kotlin:

startActivity(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))

//or 

startActivity(Intent(Settings.ACTION_SECURITY_SETTINGS))

Edit:

Opening the location settings for a specific app is not possible as of now. Instead you can open the settings screen for a specific app, and then instruct the user to go to the Permissions screen. To open the settings for a specific app, you can use the following code:

Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
Oraorabel answered 14/5, 2023 at 7:58 Comment(3)
I tried ACTION_LOCATION_SOURCE_SETTINGS before- it opens location settings for all apps, not specifically for one app. Somehow the package name needs to be provided I think.Yen
Did you try using ACTION_SECURITY_SETTINGS?Oraorabel
I need location settings, not security.Yen

© 2022 - 2024 — McMap. All rights reserved.