8

是否可以从 android 的 webview 历史记录中删除特定的 url?我用谷歌搜索了很多,但找不到任何 sol,即使我检查了 api 的文档,我认为没有直接的方法可以做到这一点。看看下面的代码:

// getting the whole list of web view's history....
WebBackForwardList list = wv.copyBackForwardList();
// getting previuous item/link 
String lastUrl = list.getItemAtIndex(list.getCurrentIndex()-1).getUrl();

从第二行代码很明显可以玩弄历史,所以我认为必须有某种方法或技巧从网络视图历史中删除特定链接。这对你们有意义吗?

任何代码片段或任何链接都将受到高度赞赏。

谢谢,

4

2 回答 2

4

Well, not sure if you actually can play around with the back forward list. I would assume it's a copy - at least that's what the name suggest.

However, you can manipulate the back stepping to some degree. In my case I exit the application already when user tries to step back from second page towards first page.

  @Override
  public boolean dispatchKeyEvent(KeyEvent event) {
    int action = event.getAction();
    int keyCode = event.getKeyCode();
    switch (keyCode) {
        case KeyEvent.KEYCODE_BACK:
            if ((action == KeyEvent.ACTION_DOWN) && myWebView.canGoBack() ) {
                WebBackForwardList list = myWebView.copyBackForwardList();
                String lastUrl = list.getItemAtIndex(list.getCurrentIndex()-1).getUrl().toString();

                // Exit if index == 1, i.e. currently on 2nd page
                if (list.getCurrentIndex() == 1) {
                    finish();
                } else ...

You could step over also other other pages by combining stuff from above and overloading url loading:

            @Override
            public boolean shouldOverrideUrlLoading(WebView  view, String  url){
                 view.loadUrl(url);
                 return true;
            }
于 2013-01-24T20:42:34.717 回答
0

问题和答案都帮助我们找到了解决我们问题的以下解决方案。

我们想跳回到特定的 url (partialUrl),它可以在 WebBackForwardList 中的任何位置。所以我们使用下面的代码去返回或转发,基于ValueString参数传入的部分Url。(我们想从任何其他线程调用该方法,所以使用 runOnUiThread )

if (!theValueString.isEmpty()) {
    final String partialUrl = theValueString;
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            // getting the whole list of web view's history....
            WebBackForwardList list = myWebview.copyBackForwardList();
            int index = list.getCurrentIndex();
            for (int i = list.getSize() - 1; i >= 0; i--) {
                String historyurl = list.getItemAtIndex(i).getUrl();
                if (historyurl.contains(partialUrl)) {
                    myWebview.goBackOrForward(i - index);
                    break;
                }
            }
        }
    });
}
于 2020-01-08T07:45:12.060 回答