main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/webview" />
</LinearLayout>
assets目录下的index.html文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script language="javascript">
function btnOnClick()
{
alert("我是被点击事件...");
}
function mytest()
{
window.myTrack.track("hhhhhhhhhhhgggggggggggfffffffffff");
}
</script>
</head>
<body>
<input type="button" name="btn_click" value="点击事件" onclick="btnOnClick();" />
<input type="button" name="btn_click" value="跟踪事件测试" onclick="mytest();" />
</body>
</html>
TestJqueryActivity类
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class TestJqueryActivity extends Activity {
/** Called when the activity is first created. */
private WebView webView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Toast.makeText(TestJqueryActivity.this, Environment.getExternalStorageDirectory()+"/serverconfig", 1000).show();
// Log.v("TestJqueryActivity:", Environment.getExternalStorageDirectory()+"/serverconfig");
webView=(WebView)findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.addJavascriptInterface(new MyTrack(), "myTrack");
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);//允许js弹出窗口
webView.loadUrl("file:///android_asset/index.html");
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result)
{
AlertDialog.Builder b2 = new AlertDialog.Builder(TestJqueryActivity.this)
.setTitle("来自网页的消息").setMessage(message)
.setPositiveButton("ok",
new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
result.confirm();
// MyWebView.this.finish();
}
});
b2.setCancelable(false);
b2.create();
b2.show();
return true;
}
});
}
}
MyTrack类
import android.util.Log;
public class MyTrack {
public void track(String message)
{
Log.v("this is my track message:", message);
}
}
这个测试项目的核心是 webView.addJavascriptInterface(new MyTrack(), "myTrack"); 这句代码,该代码可以将index.html中的参数类容传人安卓程序中,从而实现了静态网页与android的交互。addJavascriptInterface将MyTrck类作为一个标记myTrack在网页中使用,index.html中的window.myTrack.track("hhhhhhhhhhhgggggggggggfffffffffff");就相当于调用了MyTrack类中的track方法,从而讲hhhhhhhhhhhgggggggggggfffffffffff在log日志中打印出来。另外这个例子中还包括了网页在安卓环境中弹出对话框的实现,感兴趣的话自己试一试。