Android webview无法运行js function

时间:2016-12-12 09:05:16

标签: java android webview

我使用android webview,它加载页面并在加载javascript函数调用后,但它不起作用。对于测试,我运行了警报功能,它运行良好。

设置webview:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        webView = (WebView) findViewById(R.id.webView);
        webView.setWebChromeClient(new WebChromeClient());
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setJavaScriptEnabled(true);

    webView.addJavascriptInterface(new myJavaScriptInterface(), "CallToAnAndroidFunction");
    if (Build.VERSION.SDK_INT >= 19) {
        // chromium, enable hardware acceleration
        webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    } else {
        // older android version, disable hardware acceleration
        webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    webView.loadUrl("url");
    webView.setWebViewClient(new WebViewClient() {

        @Override
        public void onPageFinished(WebView view, String url) {
            // Make a note that the page has finished loading.
            authorization();
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

        }
    });

private void authorization() {
      webView.loadUrl("javascript:appReg(" + deviceId + "," + "promoCode);");
    }

不行     webView.loadUrl(" javascript:appReg(" + deviceId +"," +" promoCode);");

如果致电webView.loadUrl("javascript:alert('asd')");,则可以使用

1 个答案:

答案 0 :(得分:0)

webView.loadUrl("javascript:appReg(" + deviceId + "," + "promoCode);");

需要添加''webView.loadUrl("javascript:appReg('" + deviceId + "," + "promoCode);'");