Request permission for reading and writing in Android 13

634 Views Asked by At

as I read there was a change in the permission settings in Android. My phone is running under Android 13. I want to show a window/permission screen to the user and requesting all permissions. However, there is never a window for external storage and so the result is always false.

Here is my code which I wrote.

String[] permissions = new String[]{MANAGE_EXTERNAL_STORAGE, ACCESS_FINE_LOCATION, ACCESS_COARSE_LOCATION, POST_NOTIFICATIONS};

permissionLauncherMultiple.launch(permissions);

private ActivityResultLauncher<String[]> permissionLauncherMultiple = registerForActivityResult(
        new ActivityResultContracts.RequestMultiplePermissions(),
        new ActivityResultCallback<Map<String, Boolean>>() {
            @Override
            public void onActivityResult(Map<String, Boolean> result) {
                //here we will check if permissions were now (from permission request dialog) or already granted or not

                boolean allAreGranted = true;
                for (Boolean isGranted : result.values()) {
                    Log.d("Granted", "onActivityResult: isGranted: " + isGranted);
                    allAreGranted = allAreGranted && isGranted;
                }

                if (allAreGranted) {
                    //All Permissions granted now do the required task here or call the function for that
                    Toast.makeText(getApplicationContext(), "Granted everything", Toast.LENGTH_LONG).show();
                } else {
                    //All or some Permissions were denied so can't do the task that requires that permission
                    Log.d("Permission denied", "onActivityResult: All or some permissions denied...");
                    Toast.makeText(MapsActivity.this, "All or some permissions denied...", Toast.LENGTH_SHORT).show();
                }
            }
        }
);

Maybe someone can tell me how to show the permission screen to the user?

Thank you very much in advance!

0

There are 0 best solutions below