最近公司的App爲了加快開發效率選擇了一部分功能採用H5開發,從目前市面的大部分App來說,大體分紅Native App、Web App、Hybrid App三種方式,我的以爲目前以Hybrid App居多,單純的數據展現咱們直接採用WebView來渲染就能夠了,可是有時候可能會用到二者之間傳遞參數的狀況,今天就來總結一下二者之間如何互相調用。本篇主要介紹WebView與Javascript交互數據,關於如何將H5網頁呈如今WebView上能夠參考這篇博客:Android總結之WebView使用總結。javascript
混合開發相關博客:html
WebView與Javascript交互是雙向的數據傳遞,1.H5網頁的JS函數調用Native函數 2.Native函數調用JS函數,具體實現如下面例子爲主:java
<uses-permission android:name="android.permission.INTERNET"/>
mWebView.getSettings().setJavaScriptEnabled(true);
<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <script type="text/javascript"> function actionFromNative(){ document.getElementById("log_msg").innerHTML += "<br\>Native調用了js函數"; } function actionFromNativeWithParam(arg){ document.getElementById("log_msg").innerHTML += ("<br\>Native調用了js函數並傳遞參數:"+arg); } </script> </head> <body> <p>WebView與Javascript交互</p> <div> <button onClick="window.wx.actionFromJs()">點擊調用Native代碼</button> </div> <br/> <div> <button onClick="window.wx.actionFromJsWithParam('come from Js')">點擊調用Native代碼並傳遞參數</button> </div> <br/> <div id="log_msg">調用打印信息</div> </body> </html>
public class MainActivity extends Activity { private WebView mWebView; private TextView logTextView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mWebView = (WebView) findViewById(R.id.webview); // 啓用javascript mWebView.getSettings().setJavaScriptEnabled(true); // 從assets目錄下面的加載html mWebView.loadUrl("file:///android_asset/wx.html"); mWebView.addJavascriptInterface(this, "wx"); logTextView = (TextView) findViewById(R.id.text); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { // 無參數調用 mWebView.loadUrl("javascript:actionFromNative()"); // 傳遞參數調用 mWebView.loadUrl("javascript:actionFromNativeWithParam(" + "'come from Native'" + ")"); } }); } @android.webkit.JavascriptInterface public void actionFromJs() { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "js調用了Native函數", Toast.LENGTH_SHORT).show(); String text = logTextView.getText() + "\njs調用了Native函數"; logTextView.setText(text); } }); } @android.webkit.JavascriptInterface public void actionFromJsWithParam(final String str) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "js調用了Native函數傳遞參數:" + str, Toast.LENGTH_SHORT).show(); String text = logTextView.getText() + "\njs調用了Native函數傳遞參數:" + str; logTextView.setText(text); } }); } }
mWebView.addJavascriptInterface(this, "wx");至關於添加一個js回調接口,而後給這個起一個別名,我這裏起的名字wx(微信哈哈)。@android.webkit.JavascriptInterface爲了解決addJavascriptInterface漏洞的,在4.2之後纔有的。android
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"/> <TextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:text=""/> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Native調用js函數"/> </LinearLayout>
(1.)js(HTML)訪問Android(Java)端代碼是經過jsObj對象實現的,調用jsObj對象中的函數,如: window.jsObj.actionFromJs(),這裏的jsObj就是Native中添加接口的別名web
(2.)Android(Java)訪問js(HTML)端代碼是經過loadUrl函數實現的,訪問格式如:mWebView.loadUrl("javascript:actionFromNative()");微信
demo運行截圖:網絡
這裏簡單的實現了Js與Native的交互,後期會抽空看下WebViewJavascriptBridge這個開源框架。框架