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

时间:2015-03-22 15:21:48

标签: android google-chrome android-browser

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

2 个答案:

答案 0 :(得分:6)

ActivityService中,添加

ContentResolver cR = getContentResolver();

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

其中Browserandroid.provider.Browser类。

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

答案 1 :(得分:0)

是的,可以清除铬历史记录和从您的申请中搜索。请参阅下文。

['20', '48', '48', '48', '2', '0.5']

在清单中添加权限

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

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