我们可以在我们的 android 应用程序中获取 chrome 浏览历史记录/书签吗

问题描述:

我们能否像使用 READ_HISTORY_BOOKMARKS 权限在默认浏览器中一样获取 chrome 浏览历史记录/书签?PS:我只是想知道这可能吗?

can we get chrome browsing history/bookmarks like we get in default browser using READ_HISTORY_BOOKMARKS permission? PS:I Just want to know is it possible?

是的,很有可能.使用这个 uri:content://com.android.chrome.browser/bookmarks 而不是 Browser.BOOKMARKS_URI

Yes it is very much possible. Use this uri: content://com.android.chrome.browser/bookmarks instead of Browser.BOOKMARKS_URI

String[] proj = new String[] { Browser.BookmarkColumns.TITLE,Browser.BookmarkColumns.URL };
Uri uriCustom = Uri.parse("content://com.android.chrome.browser/bookmarks");
String sel = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history, 1 = bookmark
Cursor mCur = getContentResolver().query(uriCustom, proj, sel, null, null);
mCur.moveToFirst();
@SuppressWarnings("unused")
String title = "";
@SuppressWarnings("unused")
String url = "";

if (mCur.moveToFirst() && mCur.getCount() > 0) {
    boolean cont = true;
    while (mCur.isAfterLast() == false && cont) {
        title = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.TITLE));
        url = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.URL));
        // Do something with title and url
        mCur.moveToNext();
    }
}

尚未测试代码是否有错误,但它应该可以正常工作.重要的是知道要使用的 uri.阅读this 可能会有所帮助.

Havent tested the code for errors but it should work fine. The important thing is knowing the uri to use. Reading this and this might help.