user1406716 user1406716 - 2 months ago 6
Android Question

How to get last/currently accessed URL in Chrome?

I am running the following function 'getChromeLastURL()' in my service and checking for the last URL accessed every second (I want to catch the current URL user is accessing when Chrome or another browser is open).

Problem - The Browser history does not seem to be updated very often. It does change i can see in Log, but it takes a while, cant observe a set internal either.

Is there a way to force Browser history to refresh and get the latest URL being accessed?

private String getChromeLastURL() {
String[] proj = new String[] { Browser.BookmarkColumns.TITLE,Browser.BookmarkColumns.URL };
Uri uriCustom = Uri.parse("content://com.android.chrome.browser/bookmarks");
String sel = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history, 1 = bookmark
Cursor mCur = getContentResolver().query(uriCustom, proj, sel, null, null);
mCur.moveToFirst();
@SuppressWarnings("unused")
String title = "";
@SuppressWarnings("unused")
String url = "";

mCur.moveToLast();

//title = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.TITLE));
url = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.URL));
Log.v("focus" + " url", "Chrome URL = " + url);
return url;
}

Answer

Hate to answer my own questions, but I achieved this by doing the following:

Changed:

Cursor mCur = getContentResolver().query(uriCustom, proj, sel, null, null);

to

Cursor mCur = getContentResolver().query(uriCustom, proj, sel, null, BookmarkColumns.DATE + " ASC");
Comments