以编程方式清除Chrome浏览器历史记录 [英] Clear chrome browser history programmatically

查看:97
本文介绍了以编程方式清除Chrome浏览器历史记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用以下代码,但不适用于chrome,仅适用于旧版浏览器.

I have tried using the following code but it doesn't work with chrome, only the old browser.

Browser.clearHistory(getContentResolver());

推荐答案

此代码对我有用

private void clearHistoryChrome() {
    ContentResolver cr = getContentResolver();
    if (canClearHistory(cr)) {
        deleteHistoryWhere(cr, null);
    }
}

private void deleteHistoryWhere(ContentResolver cr, String whereClause) {
    String CONTENT_URI = "content://com.android.chrome.browser/history";
    Uri URI = Uri.parse(CONTENT_URI);
    Cursor cursor = null;
    try {
        cursor = cr.query(URI, new String[]{"url"}, whereClause,
                null, null);
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                final WebIconDatabase iconDb = WebIconDatabase.getInstance();
                do {
                    // Delete favicons
                    // TODO don't release if the URL is bookmarked
                    iconDb.releaseIconForPageUrl(cursor.getString(0));
                } while (cursor.moveToNext());
                cr.delete(URI, whereClause, null);
            }
        }
    } catch (IllegalStateException e) {
        Log.i("DEBUG_", "deleteHistoryWhere IllegalStateException: " + e.getMessage());
        return;
    } finally {
        if (cursor != null) cursor.close();
    }
    Log.i("DEBUG_", "deleteHistoryWhere: GOOD");
}

public boolean canClearHistory(ContentResolver cr) {
    String CONTENT_URI = "content://com.android.chrome.browser/history";
    Uri URI = Uri.parse(CONTENT_URI);
    String _ID = "_id";
    String VISITS = "visits";
    Cursor cursor = null;
    boolean ret = false;
    try {
        cursor = cr.query(URI,
                new String[]{_ID, VISITS},
                null, null, null);
        if (cursor != null) {
            ret = cursor.getCount() > 0;
        }
    } catch (IllegalStateException e) {
        Log.i("DEBUG_", "canClearHistory IllegalStateException: " + e.getMessage());
    } finally {
        if (cursor != null) cursor.close();
    }
    Log.i("DEBUG_", "canClearHistory: " + ret);
    return ret;
}

我认为这会对您有所帮助

I think this will help you

这篇关于以编程方式清除Chrome浏览器历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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