android - 如何在webview中浏览网页历史记录

标签 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");

    }
}

最佳答案

这是代码

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();
   }
}

关于android - 如何在webview中浏览网页历史记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22515991/

相关文章:

java - Android Studio- Java RecyclerView显示的 View (所有者)多于 session 室数据库中的行数

java - 如何一键正确地将数据保存在多个 fragment 中?

android - FloatingActionButton 在 pre-lollipop 中占用额外的空间

android.provider.Settings.System.getString(getContentResolver(), android.provider.Settings.System.TIME_12_24) 返回 null

android - 具有多个 fragment 和屏幕方向的单个 Activity

android - webView.canGoBack() 在使用 loadDataWithBaseURL() 时不起作用

android - Webview中的Android youtube网站受到干扰

javascript - android web 浏览器 b 标签不工作

java - 使用 SeekBar 的进度变化覆盖 WebView

android - 在谷歌浏览器中启动任何网站链接的目的是什么