一、概述
在每次访问一个网页的时候,webview都会记录访问的地址,保存了一个堆栈中,这就是我们要说的浏览历史记录。网页进行加载时,都会调用doUpdateVisitedHistory(WebView view, String url, boolean isReload)方法去更新历史记录。
注意:doUpdateVisitedHistory(WebView view, String url, boolean isReload)方法在网页加载过程中只会被调用一次,网页前进后退并不会回调这个函数。
二、
WebBackForwardList历史记录列表类
- 此类包含WebView的后退/前进列表。
- WebView.copyBackForwardList()将返回此类的副本,用于检查列表中的条目。
public abstract class WebBackForwardList implements Cloneable, Serializable {
/**
* Return the current history item. This method returns {@code null} if the list is
* empty.
* 返回当前历史记录项。 如果列表为空,则此方法返回null。
* @return The current history item.当前历史记录项目。
*/
@Nullable
public abstract WebHistoryItem getCurrentItem();
/**
* Get the index of the current history item. This index can be used to
* directly index into the array list.
* 获取当前历史记录项的索引。 该索引可用于直接索引到数组列表中。
* @return The current index from 0...n or -1 if the list is empty.
* return :如果列表为空,则当前索引从0 ... n或-1开始。
*/
public abstract int getCurrentIndex();
/**
* Get the history item at the given index. The index range is from 0...n
* where 0 is the first item and n is the last item.
* 获取给定索引处的历史记录项。 索引范围是0 ... n,其中0是第一项,n是最后一项。
* @param index The index to retrieve.要检索的索引。
*/
public abstract WebHistoryItem getItemAtIndex(int index);
/**
* Get the total size of the back/forward list.
* 获取后退/前进列表的总大小。
* @return The size of the list.
*/
public abstract int getSize();
/**
* Clone the entire object to be used in the UI thread by clients of
* WebView. This creates a copy that should never be modified by any of the
* webkit package classes.
*/
protected abstract WebBackForwardList clone();
}
获取历史记录列表:
-
WebBackForwardList history = mWebView.copyBackForwardList();
三、WebHistoryItem类
- 用于访问WebView的后退/前进列表中的条目中的字段的便捷类。
- 每个WebHistoryItem都是请求的历史记录项的快照。包含当前url、原始url,网页标题,Favicon等信息
public abstract class WebHistoryItem implements Cloneable {
/**
* Return an identifier for this history item. If an item is a copy of
* another item, the identifiers will be the same even if they are not the
* same object.
* @return The id for this item.
* @deprecated This method is now obsolete.
* @hide Since API level {@link android.os.Build.VERSION_CODES#JELLY_BEAN_MR1}
*/
@SystemApi
@Deprecated
public abstract int getId();
/**
* Return the url of this history item. The url is the base url of this
* history item. See getTargetUrl() for the url that is the actual target of
* this history item.
* @return The base url of this history item.
* Note: The VM ensures 32-bit atomic read/write operations so we don't have
* to synchronize this method.
* 返回此历史记录项的URL。 url是此历史记录项的基本URL。
*/
public abstract String getUrl();
/**
* Return the original url of this history item. This was the requested
* url, the final url may be different as there might have been
* redirects while loading the site.
* @return The original url of this history item.
* 返回此历史记录项的原始URL。 这是请求的URL,最终的URL可能不同,因为在加载站点时可能存在重定向。
*/
public abstract String getOriginalUrl();
/**
* Return the document title of this history item.
* @return The document title of this history item.
* Note: The VM ensures 32-bit atomic read/write operations so we don't have
* to synchronize this method.
* 返回此历史记录项的文档标题。
*/
public abstract String getTitle();
/**
* Return the favicon of this history item or {@code null} if no favicon was found.
* @return A Bitmap containing the favicon for this history item or {@code null}.
* Note: The VM ensures 32-bit atomic read/write operations so we don't have
* to synchronize this method.
* 返回此历史记录项的favicon,如果未找到favicon,则返回null。
*/
@Nullable
public abstract Bitmap getFavicon();
/**
* Clone the history item for use by clients of WebView.
*/
protected abstract WebHistoryItem clone();
}
获取当前历史记录详细信息:
-
WebHistoryItem webHistoryItem= history.getCurrentItem();
清除历史记录:
-
mWebView.clearHistory();