Android read browser history - android

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?

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.

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

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

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 :)

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

Related

How to get the browsing history in android for a particular time period

I would like to develop an android app for accessing the history of the browser with in a certain time interval by using AlaramManager, but when I access the history infomation, i'm getting the new information along with the previous history. Is there any way to to get the history of last few(5 or 10) minutes
Below is the code for accesing the browsing history
String[] proj = new String[] { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL , Browser.BookmarkColumns.DATE};
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);
//this.startManagingCursor(mCur);
mCur.moveToFirst();
String title = "";
String url = "";
String date_time="";
if (mCur.moveToFirst() && mCur.getCount() > 0) {
while (mCur.isAfterLast() == false) {
title = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.TITLE));
url = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.URL));
date_time=mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.DATE));
// Do something with title and url
System.out.println("Title="+title+"---"+url);
mCur.moveToNext();
}
}
Any help appreciated!
Append " AND " + Browser.BookmarkColumns.DATE + "BETWEEN ? AND ?" to your sel variable and pass new String[]{startdate, enddate} in selectionArgs of query method.
Modify the startdate and enddate as per your requirements. This method will return the data between to specific time period. We are just making sql query to get data between two particular time period. Ex :
String sel = Browser.BookmarkColumns.BOOKMARK + " = 0 " +" AND " + Browser.BookmarkColumns.DATE + "BETWEEN ? AND ?"; // 0 = history, 1 = bookmark
Cursor mCur = getContentResolver().query(uriCustom, proj, sel, new String[]{startdate, enddate}, null);

How to retrieve and display browser history and bookmarks in browser application [duplicate]

This question already has answers here:
Get browser history and search result in android
(2 answers)
Closed 8 years ago.
Hi friends i am new to android I don't know how to retrieve and display history in web browser.
Please guide me and share your thoughts, ideas and bookmarks too.
I have to display history in ListView
You can use getContentResolver to get the browser history as managedQuery has been depricated.
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();
String title = "";
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 in your manifest
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
For more details to access data using Cursor check Access Data using Cursor
ArrayList mTitles=new ArrayList();
ArrayList mUrls=new ArrayList();
public void getBrowserHist() {
Cursor mCur = managedQuery(Browser.BOOKMARKS_URI,
Browser.HISTORY_PROJECTION, null, null, null);
mCur.moveToFirst();
if (mCur.moveToFirst() && mCur.getCount() > 0) {
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();
}
}
}
Add below uses-permission into your manifest file.
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
Enjoy :-)

Android, How to get searched keywords from Chrome browser (Search Uri)?

I'm able to get searched keywords from default browser by following code.
List<SearchRecord> searchList = new LinkedList<SearchRecord>();
ContentResolver resolver = this.getContentResolver();
Cursor cursor = resolver.query(Browser.SEARCHES_URI, Browser.SEARCHES_PROJECTION, null, null, null);
cursor.moveToFirst();
if (cursor.moveToFirst() && cursor.getCount() > 0) {
while (cursor.isAfterLast() == false) {
SearchRecord record = new SearchRecord();
record.setKeyword(cursor.getString(Browser.SEARCHES_PROJECTION_SEARCH_INDEX));
record.setDate(cursor.getLong(Browser.SEARCHES_PROJECTION_DATE_INDEX));
searchList.add(record);
cursor.moveToNext();
}
}
Following code returns list of bookmarks
Uri uriCustom = Uri.parse("content://com.android.chrome.browser/bookmarks");
I'm looking for Uri of Chrome in order to get searched keywords. Do you have any idea what it is? Thanks
List<SearchRecord> searchList = new LinkedList<SearchRecord>();
ContentResolver resolver = this.getContentResolver();
Cursor cursor = resolver.query(Browser.SEARCHES_URI, Browser.SEARCHES_PROJECTION, ***Browser.BookmarkColumns.BOOKMARK+ " = 0"***, null, null);
cursor.moveToFirst();
if (cursor.moveToFirst() && cursor.getCount() > 0) {
while (cursor.isAfterLast() == false) {
SearchRecord record = new SearchRecord();
record.setKeyword(cursor.getString(Browser.SEARCHES_PROJECTION_SEARCH_INDEX));
record.setDate(cursor.getLong(Browser.SEARCHES_PROJECTION_DATE_INDEX));
searchList.add(record);
cursor.moveToNext();
}
}

Android API- getting browser bookmarks/history

I'd like to write a small android widget to getting bookmarks from my firefox browser, but my code:
Cursor myCursor=Browser.getAllBookmarks(main.getContentResolver());
not working. This cursor is always empty. It is very strange, because all my browsers (firefox, chrome) have a lot of bookmarks.
I have found this code:
String query = Browser.BookmarkColumns.BOOKMARK+"=1";
Cursor crs=main.getContentResolver().query( UriProvider.QUERY
, columns
, query
, null
, sortOrder
);
but efect is the same, result is empty.
I have problem with understanding nature of android.provider.Browser class. Is it a interface to database table ? What kind of data I can find in this table (bookmarks from firefox, or chrome, or both ???). When this table is synchronized with ff/chrome ?
Thanks for any suggestions...
Best regards
mario
String[] proj = new String[] { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL };
String sel = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history, 1 =bookmark
mCur = this.managedQuery(Browser.BOOKMARKS_URI, proj, sel, null, null);
this.startManagingCursor(mCur);
mCur.moveToFirst();
String title = "";
String url = "";
if (mCur.moveToFirst() && mCur.getCount() > 0) {
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();
}
}
have you added permission in AndroidManifest.xml
<uses-permission
android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
Thank you for reply.
Your code works but only for default build-in browser.
For chrome I have to use special content provider uri:
Uri uriCustom = Uri.parse("content://com.android.chrome.browser/bookmarks");
For FF the thing is more complicated, because when I try use uri:
Uri uriCustom = Uri.parse("content://org.mozilla.firefox.db.browser/bookmarks");
I get :
java.lang.SecurityException: Permission Denial: reading org.mozilla.firefox.db.BrowserProvider uri content://org.mozilla.firefox.db.browser/bookmarks from pid=xxxx, uid=xxxx requires org.mozilla.firefox.permissions.BROWSER_PROVIDER, or grantUriPermission()

can we get chrome browsing history/bookmarks in our android app

can we get chrome browsing history/bookmarks like we get in default browser using READ_HISTORY_BOOKMARKS permission?
PS:I Just want to know is it possible?
Yes it is very much possible.
Use this uri: content://com.android.chrome.browser/bookmarks instead of Browser.BOOKMARKS_URI
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 = "";
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();
}
}
Havent tested the code for errors but it should work fine. The important thing is knowing the uri to use. Reading this and this might help.

Categories

Resources