Android webview javascript seems not working on So

2019-07-27 01:15发布

问题:

I have tested my app on HTC and MI2 and some samsung devices, and it worked fine.

However on Sony devices, it is not working at all.

Java Code:

mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new TestInterface(), "jsjava");

    public class TestInterface {
        @JavascriptInterface
        public void login() {
            Log.d("TEST", "js call java ok");
        }
    }

Html and js code:

<script>
    function login(response){
        window.jsjava.login();
    }
</script>

<div class="land"><a href="javascript:login();">Login</a></div>

The log message "js call java ok" does not show up on sony devices, like the xperia.

I made sure to enable javascript support on device, but it is still not working.

Please help.

回答1:

Solved!

Add

webView.setWebChromeClient(new WebChromeClient());

after

webView.getSettings().setJavaScriptEnabled(true);

And add @JavascriptInterface to every function that will called from javascript.