0

Dear all i am just passing and returrn some value from javascript and android. I could able to pass value javascript to android. My problem is i could not able to return the value again. This is my snippet. can any body help me out

HTML and Script

     <html>
  <head>
    <script src="phonegap-1.3.0.js" type="text/javascript"></script>
    <script type="text/javascript">
        function invoke(param1,param2)
        {
            alert('Hai');
            //invoking the JavascriptBridge registered under the 'jb' namespace
            var result = jb.callMe(param1,param2);

            //doing something with the return value, it should be concatenation
            //of the two input parameters
            alert(result);

        }
    </script>
</head>
<body>
    <form id = "returning">
<h2>Demonstrating Android Javascript-To-Java Bridge</h2>

<input type="button" value="Invoke Bridge" onclick="invoke('Hello','World');"/>
    </form>
</body>

Android:

    public class ReturnAndroidValActivity extends Activity
    {
    private WebView webView;

    public ReturnAndroidValActivity()
    {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
            super.onCreate(savedInstanceState);
    }

    @Override
    protected void onStart()
    {
            super.onStart();
    }

    @Override
    protected void onResume()
    {
            try
            {
                    super.onResume();

                    //render the main screen
                   // this.setContentView(ViewHelper.findLayoutId(this, "main"));

                    //Find the WebView control
                    //this.webView = (WebView)ViewHelper.findViewById(this, "webview");
                    setContentView(R.layout.main);
                    this.webView = (WebView)findViewById(R.id.mybrowser);



                    //Enable Javascript...This is needed so that Javascript is allowed to execute
                    //inside the WebView
                    WebSettings webSettings = this.webView.getSettings();
                    webSettings.setJavaScriptEnabled(true);

                    //Register the 'Javascript Bridge' class under the 'jb' namespace
                    //this class can be invoked from the HTML/Javascript side
                    this.webView.addJavascriptInterface(new JavascriptBridge(), "jb");

                    //Register the WebChromeClient to assist with alerts/debugging
                    this.webView.setWebChromeClient(new MyWebChromeClient());

                    //Load assets/html/index.html resource into the WebView control
                    this.webView.loadUrl("file:///android_asset/www/index.html");
            }
            catch(Exception e)
            {
                    e.printStackTrace(System.out);
            }
    }

    final class JavascriptBridge
    {
            public String callMe(String param1, String param2)
            {
                    //Generate the returnValue from the bridge
                    String toastValue = param1 + "," + param2;

                    //Setup the Toast
                    Toast toast = Toast.makeText(ReturnAndroidValActivity.this, toastValue, Toast.LENGTH_LONG);

                    //Show the Toast
                    toast.show();

                    return toastValue;
            }
    }

    /**
 * Provides a hook for calling "alert" from javascript. Useful for
 * debugging your javascript.
 */
final class MyWebChromeClient extends WebChromeClient 
{
    @Override
    public boolean onJsAlert(WebView view, String url, String message, JsResult result) 
    {
        Log.d("JavascriptBridge", message);

        resu lt.confirm();
        return true;
    }




}

}

1 Answers1

2

Define one more function in the javascript:

<script>
  function my_callback_function(param){
    alert("Called with value: " + param);
  }
</script>

Then you call this function through the WebView in the native code like that:

webView.loadUrl("javascript:my_callback_function('TheValue')");
Boris Strandjev
  • 46,145
  • 15
  • 108
  • 135