Like the title. I want to delete cookie, cache of browser of Android by coding in my application. (browser not webview) Thank you!
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.
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.