0

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.

Rowan Gontier
  • 821
  • 9
  • 14

3 Answers3

2

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.

Rowan Gontier
  • 821
  • 9
  • 14
1

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
    });
}
  • 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. – Rowan Gontier May 14 '23 at 07:37
  • 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 sdk – Proloy Chandra Deb May 14 '23 at 10:33
1

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);
Dinux
  • 644
  • 1
  • 6
  • 19