Click here to Skip to main content
15,844,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In our android webview app, We ask for read_contacts, location, camera, audio permissions at a time on initialization of app. What happens is that simultaneously the webview url is also loaded. This causes some crucial data like location not to be passed to webview in the first load.

What we expect from the app is to load the webview url immediately after user allows, grants permissions for the above only and not before that. We tried using onRequestPermissionsResult for achieving this, but unable to do so. The code we have tried is as given hereunder

What I have tried:

I have used the following code to load webview url after granting permission but to no avail.
Java
@Override
 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
     if (requestCode == 1) {
         // Use this method to check if all the permissions are GRANTED.
         if (areAllPermissionsGranted(grantResults)) {
             get_location();
             asw_view.loadUrl(url);
         }
     }
 }

 // Method to check if all the results are GRANTED.
 private Boolean areAllPermissionsGranted(int[] grantResults) {
     boolean isGranted = false;
     if (grantResults.length > 0) {
         for (int grantResult : grantResults) {
             if (grantResult == PackageManager.PERMISSION_GRANTED) {
                 isGranted = true;
             } else {
                 // if a single permission is NOT_GRANTED, return from the method.
                 return false;
             }
         }
     }
     return isGranted;
 }

Shall appreciate should you help us out.
Posted
Updated 27-Jun-22 23:09pm
v2
Comments
David Crow 28-Jun-22 12:16pm    
What does your call to requestPermissions() look like?

1 solution

In your areAllPermissionsGranted you check if any permissions have not been granted, but only the ones that are being reported. You need to check them all individually to ensure you are not still waiting for one or more to be checked.

There is a good example of the correct way to do it at android-RuntimePermissions/Application/src/main/java/com/example/android/system/runtimepermissions at master · googlearchive/android-RuntimePermissions · GitHub[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900