如何清除(CSS)访问过的Android WebView历史记录? [英] How to clear the (CSS) visited history of an Android WebView?

查看:329
本文介绍了如何清除(CSS)访问过的Android WebView历史记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过清除前一个用户留下的任何私有数据来重用现有的WebView:

I try to reuse an existing WebView by clearing any private data the previous user left behind:

CookieManager.getInstance().removeAllCookie();
webview.clearHistory();
webview.clearFormData();
webview.clearCache(true);

clearHistory 似乎只是为了清除后面/转发列表,可通过API访问,但不能用于对网页内容中的链接进行着色的内部列表。

clearHistory seems only to clear the back/forward list, accessible via API, but not the internal list used for coloring links inside the web content.

我甚至尝试了以下内容,由另一个 stackoverflow答案

I even tried the following, suggested by another stackoverflow answer:

deleteDatabase("webview.db");
deleteDatabase("webviewCache.db");

我仍然没有运气:CSS :已访问选择器在重新加载页面后仍然有效。

I still have no luck: CSS :visited selectors still work after reloading the page.

另一种方法是使用API​​级别11隐私浏览功能(新构造函数参数),但后来我无法从访问中受益链接;并且不能再针对旧版本。

An alternative would be to use the API level 11 private browsing feature (new constructor argument), but then I cannot benefit from visited links at all; and can no longer target older versions.

也许某人有解决此问题的方法?感谢您的帮助。

Maybe someone has a solution for this issue? Thanks for your help.

到目前为止我得到的答案摘要:

我尝试了这两个答案,但第一个似乎清除了HTML5数据存储,而后者似乎特定于内置浏览器:

I tried these two answers, but the first seems to clear HTML5 data storage and the latter seems to be specific to the built-in browser:

WebStorage.getInstance().deleteAllData();
Browser.clearHistory(getContentResolver());

WebChromeClient.getVisitedHistory(ValueCallback< String []> callback)仅在我第一次在最近安装的应用程序中创建新的WebView后调用。

WebChromeClient.getVisitedHistory(ValueCallback<String[]> callback) is only called after the first time I create a new WebView in a recently installed application.

我试图从视图层次中删除 WebView 并创建一个新的,但不幸的是访问历史似乎为整个应用程序存储。

I tried to remove the WebView from view hierachy and create a new one, but unfortunately the visited history seems to be stored for the whole application.

推荐答案

覆盖WebChromeClient和WebViewClient ......该死的是隐藏的。

Override WebChromeClient and WebViewClient... Damn that was hidden.

我实际上不得不挖掘有点找到这个。

I actually had to dig up a bit to find this out.

WebView webView = (WebView)findViewById(R.id.myWebView);
WebChromeClient myWebChromeClient = new WebChromeClient(){
        @Override
        public void getVisitedHistory(ValueCallback<String[]> callback) {
// called during webview initialization, original implementation does strictly nothing 
// and defaults to the native method WebViewCore.nativeProvideVisitedHistory()
            String[] myUserHistory = getVisitedUrlsFromMyOwnDatabase(userId);
            callback.onReceiveValue(myUserHistory);
        }
    };
WebViewClient myWebViewClient = new WebViewClient(){
    @Override
public void doUpdateVisitedHistory(WebView view, String url,
        boolean isReload) {
// called whenever there is a new link being visited
        insertIfNotExistVisitedUrlIntoMyOwnDatabaseForUser(userId);
        super(view, url, isReload);
}
}
webView.setWebViewClient(myWebViewClient);
webView.setChromeClient(myWebChromeClient);
webView.getSettings().etc(whatever)...

我想我米差不多了。这是我管理的部分:它到目前为止完成的是删除css历史,所以我们已经到了一半。我无法让浏览器识别我在myUserHistory中提供的url格式,所以实际上这个代码所做的唯一功能是完全重置css历史记录,但它只在WebView实例化(或创建时)时调用一次没有检查),所以为了获得真正的多用户体验,你需要在每次登录时重新创建webview。

I think I'm "almost there". Here's the part I managed: what it does so far is remove css history altogether, so we're halfway there. I can't get the browser to recognize the url format I'm providing in "myUserHistory", so in effect the only feature this code does is reset css history altogether, but it's only called once when the WebView is instanciated (or created, didn't check), so for a true multiuser experience you'd need to recreate the webview at each login.

我现在的问题是我无法管理正确加载urlHistory。我的Honeycomb Xoom webview似乎忽略了我的数据。
好​​吧,我希望它适合你。对我来说只是调用callback.onReceiveValue(new String [] {});在getVisitedHistory()中就足够了。

My problem now is that I can't manage to load the urlHistory properly. My Honeycomb Xoom webview seems to ignore my data. Ah well, I hope it works for you. For me just calling callback.onReceiveValue(new String[]{}); in getVisitedHistory() will be good enough.

编辑:
我只花了20多分钟,因为我很好奇。此方法是委托给WebChromeClient(mCallbackProxy = WebChromeClient)的方法。

I just put twenty more minutes into it because I'm curious. This method is what delegates to the WebChromeClient (mCallbackProxy = WebChromeClient).

protected void populateVisitedLinks() {
     ValueCallback callback = new ValueCallback<String[]>() {
         public void onReceiveValue(String[] value) {
             sendMessage(EventHub.POPULATE_VISITED_LINKS, (Object)value);
         }
     };
     mCallbackProxy.getVisitedHistory(callback);
 }

它受WebViewCore保护,这是WebView的私有属性,没有访问者。 sendMessage委托给私有的EventHub,而WebViewCore用私有本地方法填充,其中一个似乎是在初始化期间实际调用populateVisitedLinks()方法的方法。

It's protected in WebViewCore, which is a private attribute of WebView with no accessor. The sendMessage delegates to EventHub which is private, and WebViewCore is filled with private native methods, and one of these seems to be the one actually calling the populateVisitedLinks() method during the initialization.

除非Google的某个人向WebView添加公共方法以触发人口增长,否则我恐怕实现目标几乎是不可能的。抱歉:(

Unless someone at Google adds a public method to WebView to trigger the repopulation, I'm afraid it's practically impossible to achieve your goal. Sorry :(

作为附注,所有这些本地访问历史记录处理确实让我想知道:为什么硬件制造商如此关心我们访问过哪些网址?;)< ;<<讽刺

As a side note, all these native visited history handling really makes me wonder: why do hardware manufacturers care so much about which urls we visited? ;) <<< sarcasm

这篇关于如何清除(CSS)访问过的Android WebView历史记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆