授予许可后立即加载WebView

发布于 2025-02-10 14:11:20 字数 1185 浏览 0 评论 0原文

在我们的Android WebView应用程序中,我们要求读取_contacts,位置,相机,音频权限,一次初始化应用程序。发生的事情是同时加载了WebView URL。这会导致一些关键数据,例如位置,不得在第一个加载中传递给WebView。

我们从应用程序中获得的期望是在用户允许之后立即加载WebView URL,仅在此之前授予上述权限。我们尝试使用Requestpermissionsresult来实现这一目标,但无法做到这一点。我们尝试过的代码是在此处给出的

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);
            }

        }
    }

任何帮助,将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最初的梦 2025-02-17 14:11:20

您只检查0th元素,即,grantresults [0] == packagemanager.permission_granted in requestpermissionsresult(...)。

您需要这样检查:

    @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;
    }

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;
    }
我家小可爱 2025-02-17 14:11:20

使用此库

gradle gradle:

implementation 'com.nabinbhandari.android:permissions:3.8'

然后:然后:

String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION}; //add your requested permissions to this array
String rationale = "Please provide location permission so that you can ...";
Permissions.Options options = new Permissions.Options()
    .setRationaleDialogTitle("Info")
    .setSettingsDialogTitle("Warning");

Permissions.check(this/*context*/, permissions, rationale, options, new         
PermissionHandler() {
@Override
public void onGranted() {
    // do your task here
}

@Override
public void onDenied(Context context, ArrayList<String> deniedPermissions) {
    // permission denied, block the feature.
}
});

Use this library

gradle:

implementation 'com.nabinbhandari.android:permissions:3.8'

then:

String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION}; //add your requested permissions to this array
String rationale = "Please provide location permission so that you can ...";
Permissions.Options options = new Permissions.Options()
    .setRationaleDialogTitle("Info")
    .setSettingsDialogTitle("Warning");

Permissions.check(this/*context*/, permissions, rationale, options, new         
PermissionHandler() {
@Override
public void onGranted() {
    // do your task here
}

@Override
public void onDenied(Context context, ArrayList<String> deniedPermissions) {
    // permission denied, block the feature.
}
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文