静态页-test.html(放在assets跟目录下):
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title>android WebView test</title>
<script type="text/javascript">
function callJavaMethod(obj){
//android_interface为Java提供的调用接口名称(Java代码中红色标注)
window.android_interface.javaMethod(obj.value);
}
function show(msg){
alert(msg);
}
</script>
</head>
<body style="background: gray;">
<p>
<a href="javascript:window.android_interface.javaMethod('hi msg from javascript!')">call java method </a>
<br />
<a href="http://www.baidu.com">百度一下</a>
</p>
<div id="container">
<input type="button" id="btn" value="点击我调用Java方法:)" onClick="javascript:callJavaMethod(this);" />
</div>
</body>
</html>
Activity类SliderEpubActivity.java:
package com.innover.ebook;
import java.util.Date;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
@SuppressLint("SetJavaScriptEnabled")
public class SliderEpubActivity extends Activity {
private static final String TAG="SliderEpubActivity";
private Handler handler;
private WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
setContentView(myWebView);
setData2WebView();
}
private void initView(){
myWebView=new WebView(this);
handler=new Handler();
setSupportJavaScript();
setInterface4JavaScript();
setWebViewClient();
setWebViewChromeClient();
}
private void setData2WebView(){
myWebView.loadUrl("file:///android_asset/test.html");
}
/**
* 设置WebView对象支持javascript
*/
private void setSupportJavaScript(){
WebSettings set = myWebView.getSettings();
set.setJavaScriptEnabled(true);
}
/**
* 添加java方法的调用接口,允许javascript脚本调用java方法
*/
private void setInterface4JavaScript(){
myWebView.addJavascriptInterface(new Object() {
//这里的方法是随意添加的
public void javaMethod(final String argsFromJavaScript) {
handler.post(new Runnable() {
@Override
public void run() {
showMsg("["+new Date()+"] 消息来自HTML页面:"+argsFromJavaScript);
}
});
}
}, "android_interface");//Javascript通过“android_interface”调用Java方法
}
/**
* WebView中链接的跳转
* WebViewClient主要帮助WebView处理各种通知、请求事件
*/
private void setWebViewClient(){
myWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
showMsg(url);
//直接在当前WebView中加载url
view.loadUrl(url);
return true;
//return super.shouldOverrideUrlLoading(view, url);
}
//当页面加载完成时调用Javascript方法
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
callJavascriptMethod();
}
});
}
/**
* 调用页面中的javascript方法
*/
private void callJavascriptMethod(){
myWebView.loadUrl("javascript:show('我是activity传过来的数据')");
}
/**
* 处理WebView加载网页弹出的对话框
*/
private void setWebViewChromeClient(){
myWebView.setWebChromeClient(new WebChromeClient(){
@Override
public boolean onJsAlert(WebView view, String url,
String message,JsResult result) {
showMsg(message);
//此处应该调用super的onJsAlert,否则内容无法加载,页面将显示空白
return super.onJsAlert(view, url, message, result);
}
});
}
private void showMsg(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
}
项目文件-AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tangzq"
android:installLocation="preferExternal"
android:versionCode="23"
android:versionName="1.6.2">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SliderEpubActivity" android:label="@string/app_name"
android:launchMode="singleTop" android:theme="@android:style/Theme.NoTitleBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
</manifest>