6

我最近注意到我的应用程序偶尔会出现 LAG。LAG 我的意思是它最多可能需要 40 秒,这取决于我使用的是 Wifi 还是移动数据......

我加载一个页面url,然后加载js执行:

    webView = (WebView) view.findViewById(R.id.WebView);

    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            logDebug("Loading URL: " + url);
            super.onPageStarted(view, url, favicon);
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            return WrappingClass.this.shouldOverrideUrlLoading(view, url);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
        logInfo("loading JavaScript to webview.");
        webView.loadUrl("full-js-here");

        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            logError("error code:" + errorCode);
            super.onReceivedError(view, errorCode, description, failingUrl);
        }
    });

    WebSettings webSettings = webView.getSettings();
    webSettings.setSavePassword(false);
    webSettings.setSaveFormData(false);
    webSettings.setJavaScriptEnabled(true);
    webView.requestFocus(View.FOCUS_DOWN);
    webView.loadUrl("url");

调用加载 url 后,页面显示在 web 视图中,UI 是响应式的,我可以单击按钮,返回,导航应用程序...

我试图运行的脚本没有执行,直到DEV_ACTION_COMPLETED打印到日志中,然后一切恢复正常,并且调用 onPageFinished 并立即运行脚本。问题是这可能需要 40 秒。

== 更新 ==

似乎延迟增长到某个点,然后延迟缩短到零并再次增长......它就像一个序列:0,1,2,4,8,16,32......然后开始从 0。

可能是因为我在太短的时间内用 webview 创建了新的活动吗?

有什么想法吗?

4

1 回答 1

5

We have managed to sketch up a solution for this crappy issue... no matter what I've tried I could not find a Java solution to this issue, but we did find an HTML + JavaScript solution:

Instead of waiting for the onPageFinish of the java, we inject a java script to listen to the window.onLoad event...

and there we inject our actual JavaScript... works like a charm...

For more details.

于 2013-08-19T13:37:09.473 回答