Android读取浏览器历史记录

发布于 2024-08-27 07:47:33 字数 266 浏览 6 评论 0原文

我想读取 Android 手机中的浏览器历史记录。

我做了一些文档阅读,然后我知道我们可以通过 android.provider.Browser 类读取浏览器历史记录。它有:

final static Cursor
getAllVisitedUrls(ContentResolver cr)

...返回Cursor的方法。

我可以获得处理光标的帮助,或者获取浏览器历史记录的任何示例代码吗?

I want to read browser history in Android phone.

I have done some document reading, then I come to know that we can read browser history by android.provider.Browser class. It has :

final static Cursor
getAllVisitedUrls(ContentResolver cr)

...method which returns Cursor.

May I get help to handle Cursor, or any example code to get browser history?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

自由如风 2024-09-03 07:47:33

不是真正的答案,但我可以告诉你我做了什么。

我首先克隆 浏览器存储库并尝试重现他们如何获取历史记录。
我开始得到:

拒绝权限:阅读
com.android.browser.BrowserProvider

所以我添加:

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

但它仍然给我同样的错误。我用谷歌搜索,发现了这个 Accessing Data With Android Cursors

希望有帮助。

Not really an answer but I can tell you what I did.

I first clone the browser repo and try to reproduce how they get the history.
And I started getting:

Permission Denial: reading
com.android.browser.BrowserProvider

So I added:

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

But it still is giving me the same error. I google it and I found this Accessing Data With Android Cursors.

Hope it helps.

绝對不後悔。 2024-09-03 07:47:33

ManagedQuery 已被弃用,因此请使用 getContentResolver 代替,使用以下代码:

String[] proj = new String[] { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL };
String sel = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history, 1 = bookmark
Cursor mCur = getContentResolver().query(Browser.BOOKMARKS_URI, 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();
    }
}

同时使用以下命令添加权限

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

managedQuery has been deprecated so use getContentResolver instead, use the following code:

String[] proj = new String[] { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL };
String sel = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history, 1 = bookmark
Cursor mCur = getContentResolver().query(Browser.BOOKMARKS_URI, 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();
    }
}

Also add permissions using

<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
无风消散 2024-09-03 07:47:33

对于 Lollipop 或更早版本

我可以使用以下代码获取历史记录:

Cursor mCur = activity.managedQuery(Browser.BOOKMARKS_URI,
                    Browser.HISTORY_PROJECTION, null, null, null);
            if (mCur.moveToFirst()) {
                while (mCur.isAfterLast() == false) {
                    Log.v("titleIdx", mCur
                            .getString(Browser.HISTORY_PROJECTION_TITLE_INDEX));
                    Log.v("urlIdx", mCur
                            .getString(Browser.HISTORY_PROJECTION_URL_INDEX));
                    mCur.moveToNext();
                }
            }

For Lollipop or earlier

I am able to get the history by using the following code:

Cursor mCur = activity.managedQuery(Browser.BOOKMARKS_URI,
                    Browser.HISTORY_PROJECTION, null, null, null);
            if (mCur.moveToFirst()) {
                while (mCur.isAfterLast() == false) {
                    Log.v("titleIdx", mCur
                            .getString(Browser.HISTORY_PROJECTION_TITLE_INDEX));
                    Log.v("urlIdx", mCur
                            .getString(Browser.HISTORY_PROJECTION_URL_INDEX));
                    mCur.moveToNext();
                }
            }
没企图 2024-09-03 07:47:33

这篇文章有点旧,但这里有另一个简单的解决方案,用于获取与 Android 中的 BookmarkSearch 内容提供程序相关的数据:

使用此库:https://github.com/EverythingMe/easy-content-providers

获取所有书签:

BrowserProvider browserProvider = new BrowserProvider(context);
List<Bookmark> bookmarks = browserProvider.getBookmarks().getList();

每个书签 包含所有字段,因此您可以获得所需的任何信息:
标题、网址、访问次数...

获取所有搜索历史记录:

List<Search> searches = browserProvider.getSearches().getList();

它适用于列表或光标,并且有一个示例应用程序可以查看其外观和工作方式。

事实上,它支持所有 Android 内容提供商,例如:联系人、短信、通话……
包含所有选项的完整文档: https://github.com/EverythingMe/easy -content-providers/wiki/Android-providers

希望它有帮助:)

This post is a little bit old, but here is another easy solution for getting data related to Bookmark and Search content providers in Android:

Use this lib: https://github.com/EverythingMe/easy-content-providers

Get all bookmarks:

BrowserProvider browserProvider = new BrowserProvider(context);
List<Bookmark> bookmarks = browserProvider.getBookmarks().getList();

Each Bookmark has all fields, so you can get any info you need:
title, url, visits, ...

Get all Search history:

List<Search> searches = browserProvider.getSearches().getList();

It works with lists or cursor and there a sample app to see how it looks and works.

In fact, there is support for all Android content providers like: Contacts, SMS, Calls, ...
Full doc with all options: https://github.com/EverythingMe/easy-content-providers/wiki/Android-providers

Hope it helped :)

哀由 2024-09-03 07:47:33
  public ArrayList<HistoryEntry> getBrowserHistory() {

    String title = "";
    String url = "";

    ArrayList<HistoryEntry> list = new ArrayList<HistoryEntry>();

    String[] proj = new String[] { Browser.BookmarkColumns.TITLE,
            Browser.BookmarkColumns.URL };
    String sel = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history,
                                                            // 1 = bookmark
    Cursor mCur = getContentResolver().query(Browser.BOOKMARKS_URI, proj,
            sel, null, null);
    mCur.moveToFirst();

    if (mCur.moveToFirst() && mCur.getCount() > 0) {
        boolean cont = true;
        while (mCur.isAfterLast() == false && cont) {
            HistoryEntry entry = new HistoryEntry();

            title = mCur.getString(mCur
                    .getColumnIndex(Browser.BookmarkColumns.TITLE));
            url = mCur.getString(mCur
                    .getColumnIndex(Browser.BookmarkColumns.URL));
            // Do something with title and url
            entry.setTitle(title);
                            entry.setUrl(url);
                            list.add(entry );
            Log.d("TAG", "title   " + title);
            mCur.moveToNext();
        }
    }

    mCur.close();

    return list;
}
  public ArrayList<HistoryEntry> getBrowserHistory() {

    String title = "";
    String url = "";

    ArrayList<HistoryEntry> list = new ArrayList<HistoryEntry>();

    String[] proj = new String[] { Browser.BookmarkColumns.TITLE,
            Browser.BookmarkColumns.URL };
    String sel = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history,
                                                            // 1 = bookmark
    Cursor mCur = getContentResolver().query(Browser.BOOKMARKS_URI, proj,
            sel, null, null);
    mCur.moveToFirst();

    if (mCur.moveToFirst() && mCur.getCount() > 0) {
        boolean cont = true;
        while (mCur.isAfterLast() == false && cont) {
            HistoryEntry entry = new HistoryEntry();

            title = mCur.getString(mCur
                    .getColumnIndex(Browser.BookmarkColumns.TITLE));
            url = mCur.getString(mCur
                    .getColumnIndex(Browser.BookmarkColumns.URL));
            // Do something with title and url
            entry.setTitle(title);
                            entry.setUrl(url);
                            list.add(entry );
            Log.d("TAG", "title   " + title);
            mCur.moveToNext();
        }
    }

    mCur.close();

    return list;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文