如何调用javascript函数并从android获取返回值?

时间:2014-01-22 09:10:00

标签: java javascript android

在这段代码中我返回一个字符串。我想将此字符串存储在android活动中。如何调用JavaScript函数并从android活动中获取返回值?

 <script type="text/javascript">
        function gettext(){
            return 'http://192.168.1.11/bmahtml5/images/specs_larg_2.png';
        };
        function button_clicked(){
            window.location='ios:nativecall';
        };
 </script>

1 个答案:

答案 0 :(得分:0)

这里很简单:

WebAppInterface.java

public class WebAppInterface
 {
        Context mContext;

        /** Instantiate the interface and set the context */
        WebAppInterface(Context c)
        {
            mContext = c;
        }

        public void storeText(String text)
        {
            //TODO  save text
            Toast.makeText(mContext, text, Toast.LENGTH_LONG).show();
        }
    }

WebView webview=(WebView)findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.addJavascriptInterface(new WebAppInterface(this), "Android");

现在你可以在html中调用这个方法,看看它

<script type="text/javascript">

    function saveFunction(text) 
    {
        Android.storeText(text);
    }
</script>
相关问题