以编程方式清除 chrome 浏览器历史记录
Clear chrome browser history programmatically
我试过使用以下代码,但它不适用于 chrome,只能用于旧浏览器。
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;
}
我想这对你有帮助
我试过使用以下代码,但它不适用于 chrome,只能用于旧浏览器。
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;
}
我想这对你有帮助