在弹出Dialog 显示WebView 要点:
public void onPageFinished(WebView view, String url)
{
super.onPageFinished(view, url);
//防止黑背景问题 setContentView(binding.getRoot());
}
import static android.view.View.GONE;
import static android.view.View.VISIBLE;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.constraintlayout.widget.ConstraintLayout;
import java.util.Objects;
public class FloatWebViewDialog extends Dialog {
private ConstraintLayout constraintLayoutLoading;
private WebView about_web_wb;
private FloatWebBinding binding;
private String url;
private String title;
@SuppressLint("InflateParams")
public FloatWebViewDialog(Context context) {
super(context);
}
//WebViewClient主要帮助WebView处理各种通知、请求事件
private final WebViewClient webViewClient = new WebViewClient() {
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
MyLog.i("onReceivedError:" + error.toString());
super.onReceivedError(view, request, error);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String urlstr) {
MyLog.i("拦截url:" + urlstr);
try {
if (urlstr.startsWith("http://") || urlstr.startsWith("https://"))
view.loadUrl(urlstr);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
MyLog.e("onPageFinished:" + url);
//防止黑背景问题
setContentView(binding.getRoot());
}
//拦截并加载
@SuppressLint("NewApi")
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
if (request != null && request.getUrl() != null)
MyLog.e("url :" + request.getUrl());
return super.shouldInterceptRequest(view, request);
}
};
private final WebChromeClient webChromeClient = new WebChromeClient() {
// 不支持js的alert弹窗,需要自己监听然后通过dialog弹窗
@Override
public boolean onJsAlert(WebView webView, String url, String message, JsResult result) {
AlertDialog.Builder localBuilder = new AlertDialog.Builder(webView.getContext());
localBuilder.setMessage(message).setPositiveButton("确定", null);
localBuilder.setCancelable(false);
localBuilder.create().show();
//注意:
//必须要这一句代码:result.confirm()表示:
//处理结果为确定状态同时唤醒WebCore线程
//否则不能继续点击按钮
result.confirm();
return true;
}
//加载进度回调
@Override
public void onProgressChanged(WebView view, int newProgress) {
constraintLayoutLoading.setVisibility(newProgress == 100 ? GONE : VISIBLE);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = FloatWebBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
Objects.requireNonNull(dialog.getWindow()).setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
//放在 window 的之后 先不动
// if (NotchScreenManager.getInstance().getNotchScreenInfo().hasNotch)
// NotchScreenManager.getInstance().setDisplayInNotch(dialog.getWindow());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
WindowManager.LayoutParams lp
= dialog.getWindow().getAttributes();
lp.layoutInDisplayCutoutMode =
WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
dialog.getWindow().setAttributes(lp);
}
StatusBarUtils.getInstance().hideBottomUIMenu(dialog.getWindow());
dialog.setCancelable(false);
Objects.requireNonNull(getWindow()).setBackgroundDrawableResource(R.color.black_72);
getWindow().getDecorView().setOnTouchListener((v, event) -> {
dismiss();
return true;
});
constraintLayoutLoading = binding.dialogClLoading;//进度条
binding.floatWvCloseIv.setOnClickListener(v -> dismiss());
about_web_wb = binding.floatWv;
//使用webview显示html代码
//webView.loadDataWithBaseURL(null,"<html><head><title> 欢迎您 </title></head>" +
// "<body><h2>使用webview显示 html代码</h2></body></html>", "text/html" , "utf-8", null);
// about_web_wb.addJavascriptInterface(this, "android");//添加js监听 这样html就能调用客户端
about_web_wb.setWebViewClient(webViewClient);
about_web_wb.setWebChromeClient(webChromeClient);
WebSettings webSettings = about_web_wb.getSettings();
webSettings.setJavaScriptEnabled(true);//允许使用js
webSettings.setUseWideViewPort(true); // 关键点
webSettings.setSupportZoom(true);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);//不使用缓存,只从网络获取数据.
// webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
// 支持屏幕缩放
webSettings.setSupportZoom(false);
webSettings.setBuiltInZoomControls(true);
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
about_web_wb.loadUrl(url);
if (!TextUtils.isEmpty(title))
binding.floatWvTitleTv.setText(title);
}
@Override
public void dismiss() {
super.dismiss();
}
@SuppressLint("SetJavaScriptEnabled")
public void show(String url, String title) {
this.url = url;
this.title = title;
if (!isShowing())
show();
if (binding != null) {
binding.floatWvTitleTv.setText(title);
about_web_wb.loadUrl(url);
}
}
}