Android以编程方式删除Chrome/默认浏览器cookie、历史记录和搜索

Android以编程方式删除Chrome/默认浏览器cookie、历史记录和搜索,android,google-chrome,android-browser,Android,Google Chrome,Android Browser,我喜欢这个标题。我想通过在我的应用程序中编码来删除Android浏览器的缓存cookie。(浏览器不是webview) 谢谢大家! 在您的活动或服务中,添加 ContentResolver cR = getContentResolver(); if(Browser.canClearHistory(cR)){ Browser.clearHistory(cR); Browser.clearSearches(cR); } 其中Browser是android.provider.Bro

我喜欢这个标题。我想通过在我的应用程序中编码来删除Android浏览器的缓存cookie。(浏览器不是webview)
谢谢大家!

在您的
活动
服务
中,添加

ContentResolver cR = getContentResolver();

if(Browser.canClearHistory(cR)){
    Browser.clearHistory(cR);
    Browser.clearSearches(cR);
}
其中
Browser
android.provider.Browser


这将清除默认浏览器的历史记录。

是的,可以从应用程序中清除chrome历史记录和搜索。请看下面

/**
 * 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();
    }
}
在清单中添加权限

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

我不知道是否可以使用此方法删除缓存/cookie,但如果可以获得任何进一步信息,我将发布。

无法解析方法clearHistory(android.content.contentresolver)您的设备的API级别是什么?我已清除cookie。这解决了我的问题。谢谢你的回复。为什么这被否决了。。?