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;
    }




}

}

2
  • I have answered the same question just yesterday. See here: stackoverflow.com/q/8982570/1108032 Commented Jan 25, 2012 at 10:04
  • @Boris....Ya ...But in my case plz tell where to edit....i will go through later Commented Jan 25, 2012 at 10:09

1 Answer 1

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

Comments

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.