34

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?

pnuts
  • 58,317
  • 11
  • 87
  • 139
Mitul Nakum
  • 5,514
  • 5
  • 35
  • 41

5 Answers5

29

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.

Community
  • 1
  • 1
Macarse
  • 91,829
  • 44
  • 175
  • 230
18

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" />
Aditya
  • 5,509
  • 4
  • 31
  • 51
  • this answer works! I also made an example based on this answer https://github.com/shanwu/shanwu_coding_base/tree/bookmark_record_example – shanwu Jul 14 '14 at 14:51
  • 3
    [Most things related to Bookmarks were removed in API Level 23 (Android 6.0+)](https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-bookmark-browser) – Aashish Kumar Feb 09 '18 at 04:29
9

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();
                }
            }
Mitul Nakum
  • 5,514
  • 5
  • 35
  • 41
  • 2
    Log.v("urlIdx", mCur.getString(Browser.HISTORY_PROJECTION_DATE_INDEX)); try this out. and for further info please visit http://developer.android.com/reference/android/provider/Browser.html – Mitul Nakum Apr 26 '12 at 05:24
  • 2
    This will not work in Marshmallow anymore: https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-bookmark-browser – user1406716 Oct 25 '15 at 21:32
4

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 :)

sromku
  • 4,663
  • 1
  • 36
  • 37
3
  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;
}
naidu
  • 350
  • 2
  • 8
  • 1
    [Most things related to Bookmarks were removed in API Level 23 (Android 6.0+)](https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-bookmark-browser) – Aashish Kumar Feb 09 '18 at 04:26