4

How to clear history of chrome browser. For native browser i use following code -

Browser.clearHistory(getContentResolver());

But this does not work for chrome. How can i clear chrome browser history? Is it possible?

unflagged.destination
  • 1,576
  • 3
  • 19
  • 38
  • https://code.google.com/p/chromium/issues/detail?id=138755 This link relates to bookmarks, but it may also work for chrome history (if you replace the bookmarks in the URI with history) – athor Dec 10 '13 at 19:36
  • any update since then? – keybee Feb 05 '15 at 09:19
  • Possible duplicate of [Android programmatically delete Chrome / default browser cookies, history, searches](http://stackoverflow.com/questions/29196106/android-programmatically-delete-chrome-default-browser-cookies-history-searc) – Paul Sweatte Dec 01 '16 at 17:22

2 Answers2

1

It work fo me

    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();
            }

or

    Cursor faves = managedQuery(BOOKMARKS_URI,
                requestedColumns,
                    null, null, null);
zloj
  • 86
  • 1
  • 4
  • if you need clear only history, this link help you http://stackoverflow.com/questions/33378357/clear-chrome-browser-history-programmatically/41784478#41784478 – zloj Jan 21 '17 at 21:07
0

Yes it is posssible to clear chrome history from your application. Please see below.

/**
 * 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();
    }
}

Add permission in manifest

<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
<uses-permission android:name="com.android.browser.permission.WRITE_HISTORY_BOOKMARKS"/>
Dheeraj T
  • 1
  • 2