传球和从JavaScript到Android返回值 [英] Passing and returning value from javascript to android

查看:105
本文介绍了传球和从JavaScript到Android返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的我只是路过,并returrn从JavaScript和android的一些价值。我可以能够通过价值的JavaScript到Android。我的问题是我不能够再返回值。这是我的片段。任何机构可以帮助我

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和脚本

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




}

}

推荐答案

定义在JavaScript中多了一个功能:

Define one more function in the javascript:

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

然后你在本地code一样,调用这个函数通过web视图:

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

webView.loadUrl("javascript:my_callback_function('TheValue')");

这篇关于传球和从JavaScript到Android返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆