永远不会调用WebViewClient中的onPageFinished / onPageStarted

时间:2014-08-26 20:21:30

标签: android webview webviewclient

我正在为Android编写一个小的Web应用程序,我需要从HTML页面获取一些JS变量,但是从不调用setParam(final String str)中的代码。

此外,onPageStarted(...)和onPageFinished(...)中的代码也从未被调用过,我无法想象为什么。

void startPageParse()
{
    WebView web = new WebView(context);

    web.getSettings().setJavaScriptEnabled(true);
    web.addJavascriptInterface(new Object(){
        @JavascriptInterface
        public void setParam(final String str)
        {
            setParamFromJS(str);
        }
    }, "JSInterface");

    web.setWebViewClient(new WebViewClient(){
        @Override
        public void onPageStarted(WebView view, String url, Bitmap fav)
        {
            super.onPageStarted(view, url, fav);

            Log.v("WebCLI", "Started");
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);

            view.loadUrl("javascript:JSInterface.setParam(flashvars['pl'])");

            Log.v("WebCLI", "JSCalled");
        }
    });

    web.loadUrl(SeasonURL);
    Log.v("WebCLI", "Load called");
}

如果我在“web.loadUrl(SeasonURL)”设置断点,它可以工作(!),那些行将在logcat中:

08-28 13:22:12.766    4294-4294/shirokovoi.ChromeCastSSApp W/chromium﹕ [WARNING:jni_string.cc(28)] ConvertJavaStringToUTF8 called with null string.
08-28 13:22:12.875    4294-4356/shirokovoi.ChromeCastSSApp W/chromium﹕ [WARNING:proxy_service.cc(890)] PAC support disabled because there is no system implementation
08-28 13:22:15.290    4294-4294/shirokovoi.ChromeCastSSApp V/WebCLI﹕ Load called
08-28 13:22:15.610    4294-4294/shirokovoi.ChromeCastSSApp I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@425157a8 time:786230
08-28 13:22:15.625    4294-4294/shirokovoi.ChromeCastSSApp V/WebCLI﹕ Started
08-28 13:22:20.586    4294-4294/shirokovoi.ChromeCastSSApp V/WebCLI﹕ Uncaught SyntaxError: Unexpected identifier
08-28 13:22:20.586    4294-4294/shirokovoi.ChromeCastSSApp I/chromium﹕ [INFO:CONSOLE(1)] "Uncaught SyntaxError: Unexpected identifier", source: %SOMEURL% (1)
08-28 13:22:24.469    4294-4294/shirokovoi.ChromeCastSSApp V/WebCLI﹕ JSCalled

如果此活动为主,也可以使用。

1 个答案:

答案 0 :(得分:0)

对于JavaScript调用,您将需要为KitKat及更高版本使用不同的方法。具体来说是evaluateJavaScript()。通常我会extend WebView并在WebView中实现此方法,但是如果在正确的线程上运行它,这个修改过的方法应该可以工作。以下是一种可以调用的方法,可以容纳新旧javascript调用:

public void injectJavaScript(WebView view, final String javascript) {

    if (javascript == null || javascript.isEmpty() || !this.isPageFinished) {
        return;
    }

    // As of KitKat, evaluateJavascript(String javascript) should be used over loadAd("javascript:(javaScriptMethod())")
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {

        String js = "javascript:" + javascript;
        view.loadUrl(js);
    } else {
        // Returns the value of the executed JavaScript as a JSON string.
        view.evaluateJavascript(javascript, new ValueCallback<String>() {
            @TargetApi(Build.VERSION_CODES.HONEYCOMB)
            @Override
            public void onReceiveValue(String stringCallback) {
                //If you'd like any response from the JS to confirm the execution
            }
        });
    }
}

所以,你在onPageFinished()中调用JS的地方看起来像是:

@Override
public void onPageFinished(WebView view, String url) {
    super.onPageFinished(view, url);

    String jsString = "JSInterface.setParam(flashvars['pl']);";
    injectJavaScript(view, jsString);

    Log.v("WebCLI", "JSCalled");
}