显示带时间戳的浏览历史记录

时间:2014-08-14 06:40:08

标签: android browser-history

我尝试使用以下代码获取带有时间戳的用户浏览历史记录。

获取时间戳的问题。

这是我的代码。

  public void getBrowserHist()  {
    @SuppressWarnings("deprecation")
    Cursor mCur = managedQuery(Browser.BOOKMARKS_URI,
            Browser.HISTORY_PROJECTION, null, null, null);
    StringBuffer bf=new StringBuffer(" ");
    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));
            String titleIdx=mCur
                    .getString(Browser.HISTORY_PROJECTION_TITLE_INDEX);
            String url= mCur
                    .getString(Browser.HISTORY_PROJECTION_URL_INDEX);
            bf.append(titleIdx+" "+url+"\n\n");
            mCur.moveToNext();
       }
  }
    tv.setText(bf);
 }

1 个答案:

答案 0 :(得分:1)

使用 Browser.HISTORY_PROJECTION_DATE_INDEX 获取时间和日期。下面我编辑了您的代码以获得时间戳

    public void getBrowserHist()  {
    @SuppressWarnings("deprecation")
    Cursor mCur = managedQuery(Browser.BOOKMARKS_URI,
            Browser.HISTORY_PROJECTION, null, null, null);
    StringBuffer bf=new StringBuffer(" ");
    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));
            String titleIdx=mCur
                    .getString(Browser.HISTORY_PROJECTION_TITLE_INDEX);
            String url= mCur
                    .getString(Browser.HISTORY_PROJECTION_URL_INDEX);
            //Edit
            Calendar cal = Calendar.getInstance();
            long dateTime = Long.parseLong(mCur
                        .getString(Browser.HISTORY_PROJECTION_DATE_INDEX));
            cal.setTimeInMillis(dateTime);
            Log.i("Date", cal.getTime().toString());

            //append cal.getTime().toString()
            bf.append(titleIdx+" "+url+"\n\n");
            mCur.moveToNext();
       }
  }
    tv.setText(bf);
 }