如何在webview中导航网页历史记录

时间:2014-03-19 19:15:45

标签: android android-webview

我的应用程序中包含webview。我想浏览网页历史记录,但我不知道该怎么做。请帮我。我的Main.java如下:

package com.abc.test;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    WebView wv = (WebView)findViewById(R.id.webview);
    WebSettings webSettings = wv.getSettings();
    webSettings.setJavaScriptEnabled(true);
    wv.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    });
    wv.loadUrl("http://www.google.com");

    }
}

1 个答案:

答案 0 :(得分:1)

这是代码

public void goBackInWebView(){
    WebBackForwardList history = webView.copyBackForwardList();
    int index = -1;
    String url = null;

    while (webView.canGoBackOrForward(index)) {
          if (!history.getItemAtIndex(history.getCurrentIndex() + index).getUrl().equals("about:blank")) {
             webView.goBackOrForward(index);
             url = history.getItemAtIndex(-index).getUrl();
             Log.e("tag","first non empty" + url);
             break;
           }
           index --;

    }
   // no history found that is not empty
   if (url == null) {
      finish();
   }
}