简体   繁体   中英

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.

Solved!

Add

webView.setWebChromeClient(new WebChromeClient());

after

webView.getSettings().setJavaScriptEnabled(true);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM