Android programmatically delete Chrome / default browser cookies, history, searches

和自甴很熟 提交于 2019-12-30 07:14:08

问题


Like the title. I want to delete cookie, cache of browser of Android by coding in my application. (browser not webview) Thank you!


回答1:


In your Activity or Service, add

ContentResolver cR = getContentResolver();

if(Browser.canClearHistory(cR)){
    Browser.clearHistory(cR);
    Browser.clearSearches(cR);
}

where Browser is the android.provider.Browser class.

This will clear the default browser's history.




回答2:


Yes it is possible to clear chrome history & searches 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");
    Uri searchesUri = Uri.parse("content://com.android.chrome.browser/searches");

    deleteChromeHistoryJava(cr, historyUri, null, null); 
    deleteChromeHistoryJava(cr, searchesUri, 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 permissions in manifest

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

I have no idea if we can delete the cache/cookies using this, but i will post if i can get any further information.



来源:https://stackoverflow.com/questions/29196106/android-programmatically-delete-chrome-default-browser-cookies-history-searc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!