現在市場上大大小小的app大都是Native端加上Html5端的形式,這其中就不可避免的要處理兩者之間的交互了,尤其用的多的是Android與JS的交互,那麼接下來就看看是怎麼處理的。
Android端主要代碼:
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
public class TestWebViewReadingHiddenField extends AppCompatActivity {
private static final String TAG = "ReadingHiddenField";
private Button mBtn;
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reading_hidden_filed);
mBtn = (Button) findViewById(R.id.btn);
mWebView = (WebView) findViewById(R.id.webView);
initWebView();
mBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
reLoadJs();
}
});
}
private void initWebView() {
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(new WebJsInterface(), "GetParamByJs");
mWebView.setWebViewClient(new MyWebViewClient());
mWebView.loadUrl("file:///android_asset/sample.html");
}
private class WebJsInterface {
public void getWebInfo(String message) {
Log.i("--message", message);
}
}
private class MyWebViewClient extends WebViewClient {
@Override
public void onPageFinished(WebView view, String url) {
mWebView.loadUrl("javascript:getInfoFromJs");
} }}
Html端主要代碼
<html>
<head>
<meta charset="UTF-8" content="text/html"/>
<script type="text/javascript">
function getInfoFromJs( param ){
GetParamByJs.getWebInfo(param));
}
</script>
</head>
<body>
<div>
<input type="hidden" id="aa" value="testAA">
<input type="hidden" id="bb" value="testBB">
<input type="hidden" id="cc" value="testCC">
</div>
<p>
<!-- 這個Say hello 按鈕調用Android代碼中的方法 -->
用JavaScript按鈕調用Android代碼 <br/>
<input type="button"
value="hello" onClick="getInfoFromJs(document.getElementById("aa").value)"/>
</p>
</body>
</html>
註釋的代碼是我們平常用的較多的一種獲取js裏面的值的方法,通過在Html代碼中寫一個JS方法,把隱藏域的值傳進來,對應的js代碼爲這一句:GetParamByJs.getWebInfo(message);
GetParamByJs爲addJavascriptInterface的第二個參數,getWebInfo對應第一參數對象裏的方法名,第二個參數以及getWebInfo的參數必須是通信雙方事先約定好的名稱,這樣看來差不多了,一兩個參數還不覺得繁瑣,直接在getWebInfo後面拼接幾個對應的參數名即可,但要是客戶端要獲取更多的參數,比如7,8個,那麼這種方式就不那麼好看了,而且Html端處理起來也繁瑣,那麼我們就看看第二種方式吧:
Html端不用變,只需在Android端加上這幾句代碼即可,
mWebView.addJavascriptInterface(new WebAppInterface(this), "GetShareParam");
然後看看WebAppInterface的內容
private class WebAppInterface {
Context mContext;
WebAppInterface(Context c) {
mContext = c;
}
/**
* 參數處理
*
* @param param 參數字段
* @param paramType 參數類型
*/
@JavascriptInterface
@SuppressWarnings("unused")
public void processParam(String param, int paramType) {
if (TextUtils.isEmpty(param)) {
Log.i("--param", "null or \" \"");
} else {
if (1 == paramType) {
Log.i("--aaa", param);
} else if (2 == paramType) {
Log.i("--bbb", param);
} else if (3 == paramType) {
Log.i("--ccc", param);
}
}
}
}
然後在MyWebViewClient中的onPageFinished方法裏面寫上方法:
reLoadJs()
reLoadJs方法定義如下:
// 通過外部自定義的Js方法獲取Html中的隱藏域值
mWebView.loadUrl("javascript:window.GetShareParam.processParam(document.getElementById('aa').value, 1);");
mWebView.loadUrl("javascript:window.GetShareParam.processParam(document.getElementById('bb').value, 2);");
mWebView.loadUrl("javascript:window.GetShareParam.processParam(document.getElementById('cc').value, 3);");</span>
通過這樣的方式,在Html端就不需要再專門定義一些Js方法來幫助客戶端獲取參數了,是不是簡潔方便多了^-^.當然我們這裏只是獲取了一些隱藏域值,一般用於頁面分享的需要,我們在這裏還可以通過外部js獲取html各個節點的值,具體的實現大家可以去參考js的API,好了今天就到此爲止,歡迎大家指正。