清除Chrome浏览器的历史记录

时间:2013-12-10 10:33:26

标签: android android-browser android-contentresolver

如何清除Chrome浏览器的历史记录。对于本机浏览器,我使用以下代码 -

Browser.clearHistory(getContentResolver());

但这不适用于chrome。如何清除Chrome浏览器历史记录? 有可能吗?

2 个答案:

答案 0 :(得分:1)

它对我有用

ngZone.run(_ => { router.navigate(); }

    Uri BOOKMARKS_URI = Uri.parse("content://browser/bookmarks");
    String TITLE = "title";
    String VISITS = "visits";
    String BOOKMARK = "bookmark";

    ContentResolver resolver = getContentResolver();
            try {
                Log.i("DEBUG_", "ContentResolver");
                Cursor c = resolver.query(
                        BOOKMARKS_URI,
                        null,
                        null, null, null);
                c.moveToFirst();
                Log.i("DEBUG_", "Cursor : " + c.toString());
                resolver.delete(BOOKMARKS_URI, "bookmark == 1", null);
                Log.i("DEBUG_", "ContentResolver delete ");

            } catch (IllegalStateException e) {
                Log.i("DEBUG_", "IllegalStateException  " + e.getMessage());
                e.printStackTrace();
            }

答案 1 :(得分:0)

是的,可以从您的应用程序中清除Chrome历史记录。请参阅下文。

/**
 * Clear the browser history
 */
private void clearChromeHistory(){
    ContentResolver cr = getContentResolver();
    Uri historyUri = Uri.parse("content://com.android.chrome.browser/history");
    deleteChromeHistoryJava(cr, historyUri, null, null);

}




/**
 * Delete chrome browser hisory
 * @param cr content resolver
 * @param whereClause Uri of the browser history query
 * @param projection projection array
 * @param selection selection item
 */
private void deleteChromeHistoryJava(ContentResolver cr, Uri whereClause, String[] projection, String selection) {
    Cursor mCursor = null;
    try {
        mCursor = cr.query(whereClause, projection, selection,
                null, null);
        Log.i("deleteChromeHistoryJava", " Query: " + whereClause);
        if (mCursor != null) {
            mCursor.moveToFirst();
            int count = mCursor.getColumnCount();
            String COUNT = String.valueOf(count);
            Log.i("deleteChromeHistoryJava", " mCursor count" + COUNT);
            String url = "";
            if (mCursor.moveToFirst() && mCursor.getCount() > 0) {
                while (!mCursor.isAfterLast()) {
                    url = mCursor.getString(mCursor.getColumnIndex(Browser.BookmarkColumns.URL));
                    Log.i("deleteChromeHistoryJava", " url: " + url);
                    mCursor.moveToNext();
                }
            }
            cr.delete(whereClause, selection, null);
            Log.i("deleteChromeHistoryJava", " GOOD");
        }
    } catch (IllegalStateException e) {
        Log.i("deleteChromeHistoryJava", " IllegalStateException: " + e.getMessage());
    } finally {
        if (mCursor != null) mCursor.close();
    }
}

在清单中添加权限

<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
<uses-permission android:name="com.android.browser.permission.WRITE_HISTORY_BOOKMARKS"/>