1

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

2
  • You'd just run getCustomerID();, wouldn't you? Maybe set a variable to its return value like so: var x = getCustomerID();, right? Am I misunderstanding something? Commented Dec 25, 2014 at 12:42
  • Actually i want to get that return value into my android activity. @TheWobbuffet Commented Dec 25, 2014 at 12:45

1 Answer 1

2
<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);
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for response, but it does not work. @JanHvrda
It is weird. What is the behavior when you call the window.SJSI.sendValue(0) line - I mean javascript error or program enter the sendValue() method in Java, but value is not passed correctly or program even don't enter the sendValue() method...
Should this error be about permissions or etc ? @JanHvrda
I'm not sure - may be try to add: webSettings.setDomStorageEnabled(true);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.