-
静态页-test.html(放在assets跟目录下):
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
function callJavaMethod(obj){
//android_interface为Java提供的调用接口名称(Java代码中红色标注)
window.android_interface.javaMethod(obj.value);
}
function show(msg){
alert(msg);
}
-
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:
package="com.tangzq"
android:installLocation="preferExternal"
android:versionCode="23"
android:versionName="1.6.2">
android:launchMode="singleTop" android:theme="@android:style/Theme.NoTitleBar">