Getting current url in android browser

I am looking for a way to get the current URL that the user is visiting in an Android browser application. I realized 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 is that Browser.BOOKMARKS_URI db is not updated when the user clicks back to go to the previous page in the browser and the query returns incorrect results.

See the following example:

  • user goes to www.google.com → The request returns "www.google.com"
  • user goes to www.imdb.com → The query returns "www.imdb.com"
  • user returns to return to www.google.com → The request returns "www.imdb.com" (!!)

Does anyone know how to return the correct URL that the user is viewing?

+6
source share
1 answer

I think you did not go through the following approach. try it! You can view your browsing history in the same way as for other ContentProviders. In addition to the browsing history, you can also get a 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 also need permission

 com.android.browser.permission.READ_HISTORY_BOOKMARKS 
0
source

Source: https://habr.com/ru/post/953087/


All Articles