1

我的 Android 应用升级到 API 29,所以我们需要从 Xwalkcorelibrary 更改为 WebView,运行应用时可以显示 HTML5,但此页面的内容不显示。

使用 WebView 时:

    setContentView(R.layout.bookshelf_view);
    mWebView = findViewById(R.id.web_view);
    mWebView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            view.loadUrl(request.getUrl().toString());
            return true;
        }
    });
    try {
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setUserAgentString(webSettings.getUserAgentString() + " " + AndroidCore.getInstance().getApiVersion());
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDomStorageEnabled(true);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        webSettings.setAppCacheEnabled(true);
    } catch (Exception e) {
        e.printStackTrace();
    }
    mWebView.loadUrl("file:///android_asset/HTML5/index.html");

使用 Xwalkcorelibrary 时:

 try {
        Method getBridge = XWalkView.class.getDeclaredMethod("getBridge");
        getBridge.setAccessible(true);
        XWalkViewBridge xWalkViewBridge = null;
        xWalkViewBridge = (XWalkViewBridge) getBridge.invoke(mXwalkView);
        XWalkSettings webSettings = xWalkViewBridge.getSettings();
        webSettings.setUserAgentString(
                webSettings.getUserAgentString() + " " + AndroidCore.getInstance().getApiVersion());
        webSettings.setJavaScriptEnabled(true);
        webSettings.setAllowUniversalAccessFromFileURLs(true);
        webSettings.setAppCacheEnabled(true);
    } catch (Exception e) {
        e.printStackTrace();
    }

    mXwalkView.setResourceClient(new MyResourceClient(mXwalkView));
    mXwalkView.setUIClient(new MyUIClient(mXwalkView));
    this.configureWebView();
    mXwalkView.loadAppFromManifest("file:///android_asset/manifest.json", null)

那么我做错了什么?请帮我找出解决方案?

4

2 回答 2

1

你有没有试过这样它在我的项目中为我工作:

WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    WebViewClientImpl webViewClient = new WebViewClientImpl(this);
    webView.setWebViewClient(webViewClient);


    webView.loadUrl("URL");

拦截 WebView HTTP 请求

public class WebViewClientImpl extends WebViewClient {

private Activity activity = null;

public WebViewClientImpl(Activity activity) {
    this.activity = activity;
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if(url.indexOf("jenkov.com") > -1 ) return false;

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    activity.startActivity(intent);
    return true;
}

@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
    if(url.startsWith("http://tutorials.jenkov.com/images/logo.png")){
        String mimeType = "image/png";
        String encoding = "";
        InputStream input = ...;


        WebResourceResponse response =
                new WebResourceResponse(mimeType, encoding, input);

        return response;
    }

    return null;
}
}

在 HTML 5 本地存储中缓存值

WebSettings webSettings = webView.getSettings();

webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
于 2019-11-26T04:08:20.217 回答
0

添加android:hardwareAccelerated="true"到您的清单文件。有时 webview 无法加载 video 和 iframes hardwareAccelerated

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" 
        android:hardwareAccelerated="true">

您也可以尝试更改文件网址 -

尝试改变这个 - mWebView.loadUrl("file:///android_asset/HTML5/index.html");

mWebView.loadUrl("file:///android_asset/index.html");

于 2019-11-26T04:40:21.917 回答