【问题标题】:Android programmatically delete Chrome / default browser cookies, history, searchesAndroid 以编程方式删除 Chrome/默认浏览器 cookie、历史记录、搜索
【发布时间】:2015-05-25 14:35:48
【问题描述】:

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

【问题讨论】:

  • 为什么这被否决了..?

标签: android google-chrome android-browser


【解决方案1】:

在您的ActivityService 中,添加

ContentResolver cR = getContentResolver();

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

其中Browserandroid.provider.Browser 类。

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

【讨论】:

  • 无法解析方法 clearHistory (android.content.contentresolver)
  • 您设备的 API 级别是多少?
  • 我清除了 cookie。这解决了我的错误。感谢您的回复。
【解决方案2】:

是的,可以从您的应用程序中清除 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,但如果我能获得任何进一步的信息,我会发布。

【讨论】:

    猜你喜欢
    • 2023-03-13
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 2010-09-08
    • 2017-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多