Capturing your browsing history from Android browsers in the last few minutes

I would like to capture the browsing history of all browsers on an Android phone in the last few (5 or 10) minutes. The code I'm 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 entire browsing history from the 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 a browsing history in the last few minutes to update the database. I am new to Android development. Any help would be greatly appreciated. Thanks in advance.

+6
source share
1 answer

You CANNOT get the history from ALL browsers, you can get the history for the default browser (which came with the phone).

Another browser uses a private area to store this information, there is no API.

Parameters for filling "?" sel should have a timestamp (long), and not the form yyyy / mm / dd.

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 apply any class to String, do not use ("" +) may be incorrect, for example, when you throw a float, the separator will not be localized (instead or vice versa)).

Hope this works.

+1
source

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


All Articles