Home » Android » Android read browser history

Android read browser history

Posted by: admin April 23, 2020 Leave a comment

Questions:

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?

How to&Answers:

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.

Answer:

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" />

Answer:

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

Answer:

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 🙂

Answer:

  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;
}