android 中可以通過webview來實作和js的互動,在程式中調用js代碼,隻需要将webview控件的支援js的屬性設定為true
android(java)與javascript(html)互動有四種情況:
1) android(java)調用html中js代碼
2) android(java)調用html中js代碼(帶參數)
3) html中js調用android(java)代碼
4) html中js調用android(java)代碼(帶參數)
代碼如下:
android端
mainactivity:
package com.home.jsandandroid;
import android.app.activity;
import android.os.bundle;
import android.view.keyevent;
import android.view.view;
import android.webkit.webchromeclient;
import android.webkit.websettings;
import android.webkit.webview;
import android.widget.toast;
public class mainactivity extends activity {
private webview mwebview;
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
showwebview();
}
/**
* 顯示webview并實作webview與js互動
*/
private void showwebview() {
try {
mwebview = new webview(this);
setcontentview(mwebview);
mwebview.requestfocus();
mwebview.setwebchromeclient(new webchromeclient() {
@override
public void onprogresschanged(webview view, int progress) {
mainactivity.this.settitle("loading...");
mainactivity.this.setprogress(progress);
if (progress >= 80) {
mainactivity.this.settitle("jsandroid test");
}
}
});
mwebview.setonkeylistener(new view.onkeylistener() { // webview can
// go back
public boolean onkey(view v, int keycode, keyevent event) {
if (keycode == keyevent.keycode_back
&& mwebview.cangoback()) {
mwebview.goback();
return true;
return false;
websettings websettings = mwebview.getsettings();
websettings.setjavascriptenabled(true);
websettings.setdefaulttextencodingname("utf-8");
mwebview.addjavascriptinterface(gethtmlobject(), "jsobj");
// 此html放在assets目錄下
mwebview.loadurl("file:///android_asset/test.html");
// 如果html直接來源于網頁上,可以使用下面形式
// mwebview.loadurl("http://192.168.1.121:8080/jsandroid/index.html");
} catch (exception e) {
e.printstacktrace();
}
private object gethtmlobject() {
object insertobj = new object() {
public string htmlcalljava() {
return "html call java";
}
public string htmlcalljava2(final string param) {
return "html call java : " + param;
public void javacallhtml() {
runonuithread(new runnable() {
@override
public void run() {
mwebview.loadurl("javascript: showfromhtml()");
toast.maketext(mainactivity.this, "clickbtn",
toast.length_short).show();
});
public void javacallhtml2() {
mwebview.loadurl("javascript: showfromhtml2('it-homer blog')");
toast.maketext(mainactivity.this, "clickbtn2",
};
return insertobj;
}
js(html):
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<!-- saved from url=(0032)http://localhost:8080/jsandroid/ -->
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta http-equiv="expires" content="0">
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-store,no-cache">
<meta name="handheldfriendly" content="true">
<meta name="viewport" content="width=100%; initial-scale=1.0; user-scalable=yes">
<meta name="robots" content="all">
<meta name="keywords" contect="doodle, mobile, doodlemobile, game, games">
<meta name="description" content="make people's mobile life more connected through games.">
<title>jsandroid_test</title>
<script type="text/javascript" language="javascript">
function showhtmlcalljava(){
var str = window.jsobj.htmlcalljava();
alert(str);
function showhtmlcalljava2(){
var str = window.jsobj.htmlcalljava2("it-homer blog");
function showfromhtml(){
document.getelementbyid("id_input").value = "java call html";
function showfromhtml2( param ){
document.getelementbyid("id_input2").value = "java call html : " + param;
</script>
</head>
<body>
hello it-homer
<br>
<input type="button" value="htmlcalljava" onclick="showhtmlcalljava()" />
<input type="button" value="htmlcalljava2" onclick="showhtmlcalljava2()" />
<input id="id_input" style="width: 90%" type="text" value="null" />
<input type="button" value="javacallhtml" onclick="window.jsobj.javacallhtml()" />
<input id="id_input2" style="width: 90%" type="text" value="null" />
<input type="button" value="javacallhtml2" onclick="window.jsobj.javacallhtml2()" />
</body>
</html>
代碼解析:
(1) 允許android執行js腳本設定
android(java)與js(html)互動的接口函數是: mwebview.addjavascriptinterface(gethtmlobject(), "jsobj"); // jsobj 為橋連對象
android容許執行js腳本需要設定: websettings.setjavascriptenabled(true);
(2) js(html)通路android(java)代碼
js(html)通路android(java)端代碼是通過jsobj對象實作的,調用jsobj對象中的函數,如: window.jsobj.htmlcalljava()
(3) android(java)通路js(html)代碼
android(java)通路js(html)端代碼是通過loadurl函數實作的,通路格式如:mwebview.loadurl("javascript: showfromhtml()");
說明:
1) android通路url網址,需在androidmanifest.xml檔案,配置容許通路網絡的權限:
<uses-permission android:name="android.permission.internet" />
2) android(java)調用js(html)時,使用的mwebview.loadurl("javascript: showfromhtml()");函數需在ui線程運作,因為mwebview為ui控件
public void javacallhtml(){
runonuithread(new runnable() {
@override
public void run() {
mwebview.loadurl("javascript: showfromhtml()");
toast.maketext(jsandroidactivity.this, "clickbtn", toast.length_short).show();
});
}