Search code examples
javascriptandroidwebviewsonysony-xperia

Android webview javascript seems not working on Sony Xperia


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.


Solution

  • Solved!

    Add

    webView.setWebChromeClient(new WebChromeClient());
    

    after

    webView.getSettings().setJavaScriptEnabled(true);
    

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