前言:平时项目开发时,有时H5需要APP的一些动态权限,Web支持需求

下面代码介绍下:

首先创建一个Activity:

1. 布局XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical"&gt;
    
    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" /&gt;


</RelativeLayout&gt;

2.Activity 代码

public class WebViewActivity extends AppCompatActivity {

    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        mWebView = findViewById(R.id.webView);

        mWebView.setWebChromeClient(webChromeClient);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setInitialScale(25);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setSupportZoom(true);
        webSettings.setDefaultTextEncodingName("UTF-8");
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setBuiltInZoomControls(false);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        webSettings.setLoadWithOverviewMode(true);
        mWebView.loadUrl("目标URL");
    }


    public WebChromeClient webChromeClient = new WebChromeClient() {

        @Override
        public void onPermissionRequest(PermissionRequest request) {
            PermissionUtils.permission(PermissionConstants.CAMERA, PermissionConstants.MICROPHONE, PermissionConstants.STORAGE).rationale(new PermissionUtils.OnRationaleListener() {
                @Override
                public void rationale(UtilsTransActivity activity, ShouldRequest shouldRequest) {
                    shouldRequest.again(true);
                }
            }).callback(new PermissionUtils.FullCallback() {
                @Override
                public void onGranted(List<String> permissionsGranted) {
                    request.grant(request.getResources());
                }

                @Override
                public void onDenied(List<String> permissionsDeniedForever, List<String> permissionsDenied) {
                    if (!permissionsDeniedForever.isEmpty()) {
                        PermissionUtils.launchAppDetailsSettings();
                    }
                }
            }).request();

        }
    };
}

总结动态设置:setWebChromeClient(@Nullable WebChromeClient client)

当H5获取对应权限的时候,会走 onPermissionRequest(PermissionRequest request)回调,

这个时候客户端Check 下有没有动态权限,如果没有动态获取权限然后调用

request.grant(request.getResources());

动态给H5授权,这样H5就可以获取到动态的权限,实现对应的功能

原文地址:https://blog.csdn.net/Jason_HD/article/details/128498137

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_13621.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注