Search code examples
androidandroid-webviewandroid-permissions

loading webview immediately after granting of permission


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

if (!check_permission(4) || !check_permission(3)) {
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.READ_CONTACTS, Manifest.permission.POST_NOTIFICATIONS, Manifest.permission.WRITE_CONTACTS, Manifest.permission.READ_PHONE_NUMBERS,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.RECORD_AUDIO,Manifest.permission.CAMERA,Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},
                    contact_perm);

        }

@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){
        if (requestCode == 1) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                get_location();
asw_view.loadUrl(url);
            }

        }
    }

Any help would be appreciated.


Solution

  • You're checking only for 0th element, i.e., grantResults[0] == PackageManager.PERMISSION_GRANTED in onRequestPermissionsResult(...).

    You need to check like this:

        @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);
                } else {
                    /*
                    * NOTE:
                    * -----
                    * Add a Log here to check if all the permissions are granted. 
                    * If this block doesn't executes, it means all the permissions are granted,
                    * something else is wrong inside your 'if' block and you need to debug that block.
                    * */
                }
            }
        }
        
        // 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;
        }