Android与Javascript互调

首先设置WebView的属性,如果需要该Webview能与JavaScript直接进行交互,必须将其setJavaScriptEnabled设置为true。如下面几行代码

webview = (WebView) this.findViewById(R.id.webView1);  
WebSettings webSetting = webview.getSettings();  
webSetting.setJavaScriptEnabled(true);// 是否支持javaScript  
1.对Webview绑定Java接口供javascript调用
contentWebView.addJavascriptInterface( this "wst" ); 

绑定本类this,对javascript提供接口wst,本类this中的方法任意调用 例如"window.wst.Helloworld()";获取提供以下接口:

webview.addJavascriptInterface(new JavaScriptinterface(), "android");

Java中相关接口:

  1. class JavaScriptinterface{  
            @JavascriptInterface  
            public void getResult(String str){  
                Log.e("","result = " + str);  
            }  
        }  


调用如下:

javascript:android.getResult(msg); 


猜你喜欢

转载自blog.csdn.net/nishuodeqianshou/article/details/72900000
今日推荐