alicanbatur
alicanbatur

Reputation: 2200

How to get value from javascript function loaded on webview in android

I have to say that, i look up a lot but i couldn't find the answer although there are lots of answer. They did not work for me.

I have a javascript function called getCustomerID() in my website which is loaded to my webview. It just return an id value as integer.

How can i get this return value ?

Here is function loaded in webview :

<script type="text/javascript">
    function getCustomerID(){
        return 0;
    }
</script>

And i just want to get that "0".

Any help will be appreciated

Upvotes: 1

Views: 550

Answers (1)

Quark
Quark

Reputation: 1618

<script type="text/javascript">
    function getCustomerID(){
        window.SJSI.sendValue(0);
        return 0;
    }
</script>

And implement:

...
webView.addJavascriptInterface(new SimpleJavaScriptInterface(), "SJSI");

final class SimpleJavaScriptInterface 
{
    @JavascriptInterface
    public void sendValue(final String value) 
    {
        myvalue = value;
        ...
    }
}

and don't forget:

WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);

Upvotes: 2

Related Questions