前言:
在做一个项目中需要加载本地的html页面,网上找了蛮多,都是说直接用WebView加载就可以了,所以自己也用这个方法试了下,在官方文档的指导下,记一篇,以便不时之需。。。
WebView
概述
是一个用来显示Web页面的视图控件,可以在Activity中显示一些简单在线内容,利用WebKit渲染引擎显示网页,包括向前向后的页面导航,放大缩小文本搜索等等功能。
使用方式
在准备加载在线页面时,需要添加Internet访问权限。
<uses-permission android:name="android.permission.INTERNET" />
在Android中访问一个网页的方式有很多,可以打开手机的浏览器,也可以在Apps中创建自己的Activity并显示,这要根据具体的需求进行选择。
1. 直接调起手机浏览器访问页面:通过事件执行以下代码
Uri uri = Uri.parse("http://www.example.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
2. 使用WebView进行显示Web页面
首先需要在布局文件中添加WebView,或者通过代码直接将WebView设置为Activity的内容布局
代码添加:
WebView webview = new WebView(this);
setContentView(webview);
布局添加:该布局作为例子,比较简单,可以根据你的需求进行变化。
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent">
</WebView>
然后就可以使用WebView实例进行Web页面加载:例子使用加载本地的html文件
setContentView(R.layout.activity_browser);
WebView webView = (WebView) this.findViewById(R.id.webview);
webView.loadUrl("file:///android_asset/readme_agreement.html");
在使用WebView的时候,可以自定义一些功能:
1. 继承并实现WebChromeClient类:实现例如进度更新,JavaScript警告等;
2. 继承并实现WebChromeClient类:实现例如页面的重定向等;
3. 修改一些Web设置,放大缩小等;
4. 设置JavaScript的回调接口,传递Java对象给JavaScript页面;
//1.
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
activity.setProgress(progress * 1000);
}
});
//2.
webview.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
// 3.
webview.getSettings().setJavaScriptEnabled(true);
Building Web Apps in WebView
译文:
官方的API Guides指导我们如何通过WebView构建一个网页App,没有浏览器复杂的功能,仅仅显示一个网页,场景一:显示应用中的一些用户协议或者用户指南类似的信息;场景二:用于上网进行数据检索,并加载自己的网页页面;
加载网页上面已经介绍过了,下面介绍下如何使用JavaScript:
1. 首先要开启JavaScript,默认情况下是禁用JavaScript,获取WebView的设置信息,开启JavaScript,例如:
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
代码中的WebSettings提供了很多有用的设置信息,以便于你进行使用。
2. 绑定JavaScript代码于Android代码
JavaScript可以通过接口调用Android内部的代码,实现一些功能。比如,Android可以定制一些接口,供JavaScript调用,然后JavaScript通过addJavascriptInterface()绑定的实例,调用Java代码。例如:
// Java接口
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** Show a toast from the web page */
@JavascriptInterface
public void showToast(String toast) {
Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
}
}
//传递Java接口实例,和接口调用名称
WebView webView = (WebView) findViewById(R.id.webview);
webView.addJavascriptInterface(new WebAppInterface(this), "Android");
//JavaScript使用Java接口
<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />
<script type="text/javascript">
function showAndroidToast(toast) {
Android.showToast(toast);
}
</script>
要注意的是:Android目标版本大于等于17后,需在调用的接口方法中添加@JavascriptInterface接口,当然在使用时,也可能存在客户端代码被html代码劫持的问题,引起安全漏洞。
3. 处理页面导航
默认情况下,在WebView页面中点击一个链接,会调起手机浏览器或者其他应用处理的url,通过为Web View 设置一个WebViewClient可以实现在WebView中打开这个链接。例如:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new WebViewClient());
或者我们也可以继承WebViewClient,创建一个WebViewClient,实现页面的重定向功能,
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
4.页面历史导航
当WebView中的页面进行重定向后,会自动将上一个页面放入历史访问页面中,通过向前goForeward()或向后goBack()进行页面导航。例如:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, bubble up to the default
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}
API Reference
My Sample
//todo