main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<WebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"
</RelativeLayout>
MainActivity如下:
package c.c;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
* Demo描述: WebView超时的处理
public class MainActivity extends Activity {
private Timer mTimer;
private WebView mWebView;
private TimerTask mTimerTask;
private final int TIMEOUT = 10000;
private final int TIMEOUT_ERROR = 9527;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
if (msg.what == TIMEOUT_ERROR) {
System.out.println("===》 收到消息: webView超时");
private void init() {
mWebView = (WebView) findViewById(R.id.webView);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.requestFocus();
mWebView.loadUrl("your url");
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(view, url);
@Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
System.out.println("======> onPageStarted 开始加载");
mTimer = new Timer();
mTimerTask = new TimerTask() {
@Override
public void run() {
// 在TIMEOUT时间后,则很可能超时.
// 此时若webView进度小于100,则判断其超时
// 随后利用Handle发送超时的消息
System.out.println("======> mWebView.getProgress()="+ mWebView.getProgress());
if (mWebView.getProgress() < 100) {
Message msg = new Message();
msg.what = TIMEOUT_ERROR;
mHandler.sendMessage(msg);
if (mTimer != null) {
mTimer.cancel();
mTimer.purge();
if (mWebView.getProgress() == 100) {
System.out.println("======> 未超时");
if (mTimer != null) {
mTimer.cancel();
mTimer.purge();
mTimer.schedule(mTimerTask, TIMEOUT, 1);
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
System.out.println("======> onPageFinished 加载完成");
if (mTimer != null) {
mTimer.cancel();
mTimer.purge();
// 处理Back键
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == event.KEYCODE_BACK && mWebView.canGoBack()) {
mWebView.goBack();
return true;
return super.onKeyDown(keyCode, event);
页面缓存:加载一个网页时的html、JS、CSS等页面或者资源数据,这些缓存资源是由于浏览器
的行为而产生,开发者只能通过配置HTTP响应头影响浏览器的行为才能间接地影响到这些缓存数据。