I am making a small Java application to show which URLs in Chrome that the user has visited the most. How would I access my browser history within Java?
Create a ContentObserver
class ...
static class ChromeOberver extends ContentObserver {
public ChromeOberver(Handler handler) {
super(handler);
}
@Override
public void onChange(boolean selfChange) {
onChange(selfChange, null);
}
@Override
public void onChange(boolean selfChange, Uri uri) {
super.onChange(selfChange);
Log.d(TAG, "onChange: " + selfChange);
Cursor cursor = context.getContentResolver()
.query(CHROME_BOOKMARKS_URI, new String() {"title", "url"},
"bookmark = 0", null, null);
// process cursor results
}
}
and register that class to monitor history/bookmark changes:
private static String CHROME_BOOKMARKS_URI =
"content://com.android.chrome.browser/bookmarks";
ChromeOberver observer = new ChromeOberver();
resolver.registerContentObserver(CHROME_BOOKMARKS_URI, true, observer);
Don't forget the permission:
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />