9

I'm searching for a way to get the current URL a user is visiting on the android browser application. I figured out that I can get the last visited URL from the Browser.BOOKMARKS_URI database using the following technique:

Cursor cursor = context.getContentResolver().query(Browser.BOOKMARKS_URI,
        Browser.HISTORY_PROJECTION, null, null,
        Browser.BookmarkColumns.DATE + " DESC");
cursor.moveToNext();
String url = cursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
cursor.close();

The problem with this, is that the Browser.BOOKMARKS_URI db doesn't get updated when the user presses back in order to navigate to the previous page in the browser, and the query returns incorrect results.

See the following example:

  1. user navigates to www.google.com -> Query returns "www.google.com"
  2. user navigates to www.imdb.com -> Query returns "www.imdb.com"
  3. user presses back to return to www.google.com -> Query returns "www.imdb.com" (!!)

Does anyone have any idea how to return the correct url a user is viewing?

Michael Gregorov
  • 541
  • 1
  • 6
  • 14
  • The content resolver is working as expected: press back does not reload the `www.google.com` page, but reads it from the cache. Same happens when the user switches the tab: the history shows the latest loaded URL, not necessarily the one that is displayed. You can keep track of current URL in a WebView embedded into your app. – Alex Cohn Mar 13 '14 at 11:22
  • I'm not working with a WebView embedded in my app. I'm trying to find the current web page of the Android browser app. – Michael Gregorov Mar 13 '14 at 12:58
  • I think thats outside of a normal app function, usually you cant access another app data unless it provides it by some way, getting browser history seems the only thing you can do. – Nanoc Jun 19 '15 at 11:54

1 Answers1

0

I think you did not have gone through the following approah. once try it! You can access Browsing history the same way you do that for other ContentProviders. Besides browsing history you can also get list of Bookmarks.

Cursor webLinksCursor = getContentResolver().query(Browser.BOOKMARKS_URI, Browser.HISTORY_PROJECTION, null, null, Browser.BookmarkColumns.DATE + " DESC");
int row_count = webLinksCursor.getCount();

int title_column_index = webLinksCursor.getColumnIndexOrThrow(Browser.BookmarkColumns.TITLE);
int url_column_index = webLinksCursor.getColumnIndexOrThrow(Browser.BookmarkColumns.URL);

if ((title_column_index > -1) && (url_column_index > -1) && (row_count > 0))
{
    webLinksCursor.moveToFirst();
    while (webLinksCursor.isAfterLast() == false)
    {
        if (webLinksCursor.getInt(Browser.HISTORY_PROJECTION_BOOKMARK_INDEX) != 1)
        {
            if (!webLinksCursor.isNull(url_column_index))
            {
                Log.i("History" , "Last page browsed " + webLinksCursor.getString(url_column_index));
                break;
            }
        }
        webLinksCursor.moveToNext();
    }            
}
webLinksCursor.close();

and you need the permission also

com.android.browser.permission.READ_HISTORY_BOOKMARKS
Bala Subramanyam
  • 185
  • 1
  • 2
  • 17
  • You are basically doing the same thing that I do, which is to go through the `Browser.BOOKMARKS_URI` database. The only difference is that I get only the most recent url, and you get all the urls. And as I written in my question, the `Browser.BOOKMARKS_URI` database doesn't hold the current url after a user presses back. – Michael Gregorov Sep 03 '13 at 21:01
  • I have an idea, what about the JQuery mobile for this operation? – Bala Subramanyam Sep 03 '13 at 21:38
  • JQuery mobile will help me only if I write my own browser application, but i'm trying to get the current url from the native\chrome android browser. – Michael Gregorov Sep 03 '13 at 21:55
  • @MichaelGregorov: Hey how did you you solve your issue? Actually I am looking for answer to similar problem. Thanks – Sam Feb 06 '14 at 07:23
  • 1
    @Sam: Unfortunately, I wasn't able to solve it. I did manage to get better results with the chrome browser content provider. The current url after the user pressed back was registered in the DB, but not when the user switched between open tabs. If you'll somehow find a solution, i'll be happy to hear about it. – Michael Gregorov Feb 06 '14 at 10:51
  • @MichaelGregorov I know this is very old post wanted know if you were able to crack the back and tab switch – jan_kiran Jan 19 '22 at 11:04