【问题标题】:Android WebView shows a blank page When play Html with iframe使用 iframe 播放 Html 时,Android WebView 显示空白页
【发布时间】:2020-05-19 06:39:36
【问题描述】:

我正在尝试将 html 文件显示在 android 的 WebView 中,但是当我尝试它时,我在我的 android 手机上看到黑屏,Html 有 iframe

HTML代码

<iframe style="width:100%; height:100%;" id="webPage" frameborder="0" src="http://www.youtube.com/embed/XGSy3_Czz8k">
</iframe>


<script>
    var Url, Interval;

    function init() {
        document.getElementById('webPage').setAttribute('src', Url);
    }
    function setParams(params) {
        //console.log(params);
        params.forEach(element => {
            switch (element.key) {
                case "Url":
                Url = element.value;
                    break;
                case "Interval":
                Interval = element.value;
                    break;
            }
        });
        document.getElementById('webPage').setAttribute('src', Url);

        setInterval(function () {
            document.getElementById('webPage').setAttribute('src', Url);
        }, (Interval * 60 * 1000));
    }

    function listener(event) {
        setParams(event.data);
    }
    if (window.addEventListener) {
        window.addEventListener('message', listener, false);
    } else if (window.attachEvent) { // ie8
        window.attachEvent('onmessage', listener);
    }

    function deleteContent()

{ 尝试 { var el = document.getElementById('webPage'); el.setAttribute('src',"about:blank"); el.parentNode.removeChild(el); } 抓住(错误) { console.log("网页删除错误:"+err); } }

安卓代码

browser.setInitialScale(60);
//WebView browser = new WebView(context);
browser.getSettings().setJavaScriptEnabled(true);
browser.loadUrl( "file:///android_asset/index.html");
browser.setWebChromeClient(new WebChromeClientCustomPoster());
//((Activity) context).setContentView(browser);
WebSettings ws = browser.getSettings();
ws.setAllowFileAccess(true);
ws.setAllowFileAccessFromFileURLs(true);
ws.setAllowUniversalAccessFromFileURLs(true);
ws.setJavaScriptEnabled(true);
ws.setJavaScriptCanOpenWindowsAutomatically(true);
ws.setAllowContentAccess(true);
ws.setDomStorageEnabled(true);

browser.setWebViewClient(new WebViewClient(){

    @SuppressLint("SyntheticAccessor")
    public void onPageFinished(WebView view, String url){
        // do your stuff here
        JSONObject userData = new JSONObject();

        try {
            userData.put("PlayerCode", CommonUseFunc.getJsonText("Player","PlayerCode"));
            userData.put("PrivateKey",CommonUseFunc.getJsonText("Player","PrivateKey"));
            userData.put("PublicKey",CommonUseFunc.getJsonText("Player","PublicKey"));
            userData.put("Api",CommonUseFunc.getJsonText("General","ApplicationServer","API"));
            userData.put("VideoMode","0");
            userData.put("BasePath", Objects.requireNonNull(context.getExternalFilesDir(null)).getAbsolutePath());
        } catch (JSONException e) {
            e.printStackTrace();
        }


        JSONObject jsonObjectInit = new JSONObject();
        try{
            jsonObjectInit.put("MessageType","init");
            jsonObjectInit.put("Data",userData);

            browser.evaluateJavascript("javascript:PIXAGE.Platform.WebMethod.receiveMessage('" + jsonObjectInit + "')",
                    null);

            Utils.logProcess(Utils.timeInfo(), "StartManager", "initialize Web View", 0);

        }catch (JSONException e){
            e.printStackTrace();
            Utils.logProcess(Utils.timeInfo(), "StartManager", "initialize Web View Eror", 1);
        }


        JSONObject jsonObject = new JSONObject();
        JSONObject obj = null;
        try {
            obj = new JSONObject(getPublishment);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            jsonObject.put("Guid",1111);
            jsonObject.put("MessageType","newPublishment");
            jsonObject.put("Data",obj);

            browser.evaluateJavascript("javascript:PIXAGE.Platform.WebMethod.receiveMessage('" + jsonObject + "')",
                    null);

            Utils.logProcess(Utils.timeInfo(), "StartManager", "Data Sended", 0);

        } catch (JSONException e) {
            e.printStackTrace();
            Utils.logProcess(Utils.timeInfo(), "StartManager", "Data Sended Eror", 0);
        }
    }
});

【问题讨论】:

    标签: android iframe webview


    【解决方案1】:

    试试这个 -->

    制作自定义网页视图:

    public class TouchyWebView extends WebView {
    
    public TouchyWebView(Context context) {
        super(context);
    }
    
    public TouchyWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    public TouchyWebView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event){
        requestDisallowInterceptTouchEvent(true);
        return super.onTouchEvent(event);
    }}
    

    将它包含在您的 xml 中(代替 webview):

     <com........TouchyWebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webview"
        />
    

    在活动中添加这个:

    ws.loadDataWithBaseURL(null,"file:///android_asset/index.html", "text/html", "UTF-8", null);
    
        WebSettings settings = ws.getSettings();
        WebSettings webSetting = ws.getSettings();
    
        webSetting.setBuiltInZoomControls(true);
        settings.setDomStorageEnabled(true);
        ws.getSettings().setJavaScriptEnabled(true);
        ws.setVerticalScrollBarEnabled(true);
        ws.setHorizontalScrollBarEnabled(true);
        ws.setAllowFileAccess(true);
        ws.setAllowFileAccessFromFileURLs(true);
        ws.setAllowUniversalAccessFromFileURLs(true);
         ws.setJavaScriptEnabled(true);
         ws.setJavaScriptCanOpenWindowsAutomatically(true);
         ws.setAllowContentAccess(true);
         ws.setDomStorageEnabled(true);
    

    【讨论】:

      猜你喜欢
      • 2012-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多