javascript - Returning String value from Android Activity to JS function -


i trying return string value js function android activity. js function first calls android activity method , method return string value not working ... take on code

<script type="text/javascript">    function picmethod(){      var path = activity.getpic();   alert(path);      }      </script> 

here android activity method

 public string getpic()     {  string temp="abcd"; return temp;     } 

depending on use case, there different ways of accomplishing this. difficulty lies in want things in onload method.

if possible pass in string after page loaded, use

string jsstring = "javascript:adddata('" + thestring + "');"); webview.loadurl(jsstring); 

if need data accessible on onload method of page, modify url called to include query data if possible. like:

string urlwithdata = yoururl + "?data=" + thestring; webview.loadurl(urlwithdata); 

and use standard javascript parse window.location.search data.

finally, if can't modify url reason, can use callback object let javascript value:

private class stringgetter {    public string getstring() {        return "some string";    } } 

and when config webview callback before loading url:

webview.addjavascriptinterface(new stringgetter(), "stringgetter"); 

and in onload method of page use:

var thestring = stringgetter.getstring(); 

hope helps!

regards:@albin


Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -