前言:平时项目开发时,有时H5需要APP的一些动态权限,Web已支持该需求
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">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
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进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。