在过去几分钟内从Android浏览器捕获浏览历史记录

时间:2015-04-24 11:49:13

标签: android browser-history

我想在最近几(5或10)分钟内捕获Android手机中所有浏览器的浏览历史记录。我正在使用的代码是

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

}

如果我更改

,我可以从Chrome浏览器获取整个浏览历史记录
String sel = Browser.BookmarkColumns.BOOKMARK + " = 0" +" AND " +Browser.BookmarkColumns.DATE+ " BETWEEN ? AND ?"; // 0 = history, 1 = bookmark 

String sel = Browser.BookmarkColumns.BOOKMARK; // 0 = history, 1 = bookmark

但是我需要最近几分钟的浏览历史来更新数据库。我是android开发的新手。任何帮助将不胜感激。提前谢谢..

1 个答案:

答案 0 :(得分:1)

您无法从所有浏览器获取历史记录,您只需获取默认浏览器的历史记录(随手机附带)。

其他浏览器使用私有作用域来保存此信息,没有API。

准备填充" " on sel 需要是时间戳(长),而不是yyyy / mm / dd格式。

只是做:

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

注意:使用String.valueOf将任何类强制转换为String,不要使用("" +)可能是错误的,例如当转换为float时,分隔符将不会被定位(反而。或反之亦然)。

希望它有效。