I would like to capture browsing history of all the browsers in an android phone for the last few (5 or 10) minutes. The code i am using is
Calendar ci = Calendar.getInstance();
String enddate = "" + ci.get(Calendar.YEAR) + "-"
+ (ci.get(Calendar.MONTH) + 1) + "-"
+ ci.get(Calendar.DAY_OF_MONTH) + " " + ci.get(Calendar.HOUR)
+ ":" + ci.get(Calendar.MINUTE) + ":" + ci.get(Calendar.SECOND);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ci = Calendar.getInstance();
ci.add(Calendar.MINUTE, -5);
String startdate = "" + ci.get(Calendar.YEAR) + "-"
+ (ci.get(Calendar.MONTH) + 1) + "-"
+ ci.get(Calendar.DAY_OF_MONTH) + " " + ci.get(Calendar.HOUR)
+ ":" + ci.get(Calendar.MINUTE) + ":" + ci.get(Calendar.SECOND);
long startdates = 0;
long enddates = 0;
try {
Date endDate = (Date) new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.parse(enddate);
Date startDate = (Date) new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
.parse(startdate);
System.out.println("End date=" + endDate.getTime());
startdates = startDate.getTime();
enddates = endDate.getTime();
} catch (ParseException e) {
e.printStackTrace();
}
String[] proj = new String[] {
Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL,
Browser.BookmarkColumns.DATE
};
Uri uriCustom = Uri
.parse("content://com.android.chrome.browser/bookmarks");
// 0 = history, 1 = bookmark
String sel = Browser.BookmarkColumns.BOOKMARK + " = 0" + " AND "
+ Browser.BookmarkColumns.DATE + " BETWEEN ? AND ?";
Cursor mCur = getContentResolver().query(uriCustom, proj, sel,
new String[] {
"" + startdates, "" + enddates
}, null);
mCur.moveToFirst();
JSONObject obj;
JSONArray jarray = new JSONArray();
String title = "";
String burl = "";
String date_time = "";
if (mCur.moveToFirst() && mCur.getCount() > 0) {
while (mCur.isAfterLast() == false) {
title = mCur.getString(mCur
.getColumnIndex(Browser.BookmarkColumns.TITLE));
burl = mCur.getString(mCur
.getColumnIndex(Browser.BookmarkColumns.URL));
date_time = mCur.getString(mCur
.getColumnIndex(Browser.BookmarkColumns.DATE));
try {
obj = new JSONObject();
obj.put("title", title);
obj.put("url", burl);
obj.put("date", date_time);
jarray.put(obj);
} catch (Exception e)
{
e.printStackTrace();
}
mCur.moveToNext();
System.out.println(title);
}
}
I can get the whole browsing history from chrome browser if i change
String sel = Browser.BookmarkColumns.BOOKMARK + " = 0" +" AND " +Browser.BookmarkColumns.DATE+ " BETWEEN ? AND ?"; // 0 = history, 1 = bookmark
to
String sel = Browser.BookmarkColumns.BOOKMARK; // 0 = history, 1 = bookmark
but i need the browsing history for the last few minutes to update database. I am new to android developing. Any help would be greatly appreciated. Thanks in advance..
You CANNOT get the history from ALL the browsers, you just can get the history for the default browser(which came with the phone).
The other browser use private scope to keep this info, there is no API.
The params to fill the "?" on sel need to be a timestamp (long), not a yyyy/mm/dd format.
Just do:
final Calendar ci = Calendar.getInstance();
final long endDate = ci.getTimeInMillis();
ci.add(Calendar.MINUTE, -5);
final long startDate = ci.getTimeInMillis();
final String[] paramsSel = new String[] { String.valueOf(startDate), String.valueOf(endDate) };
Note: use String.valueOf to cast any class to a String, don't use (""+) could be wrong, for example when cast a float, the separator will be not be localiced (, instead . or viceversa).
Hope it's works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With