3

我正在编写一个应用程序,它必须帮助我获取有关浏览器历史记录的所有信息,所以我编写了一个简单的代码:

public class WebHistory {
    private Context context;
    private Cursor cr;
    public StringBuilder sb;
    public WebHistory(Context c){
        this.context = c;
    }

    public void takeHistory(){
        cr = context.getContentResolver().query(Browser.BOOKMARKS_URI,Browser.HISTORY_PROJECTION, null, null, null);
        cr.moveToFirst();
        String title = "";
        String date = "";
        String visits = "";
        String url = "";
        String info = "";
        if(cr.moveToFirst() && cr.getCount() > 0){
            while(cr.isAfterLast() == false){
                title = cr.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX);
                date = cr.getString(Browser.HISTORY_PROJECTION_DATE_INDEX);
                url = cr.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
                visits = cr.getString(Browser.HISTORY_PROJECTION_VISITS_INDEX);
                info = title + " date: " + date + " url: " + url + " visits" + visits + "\n";
                Toast.makeText(context, info, Toast.LENGTH_LONG).show();
                cr.moveToNext();
            }
        }


    }
}

方法takeHistory()可以帮助我获取一些有关浏览器历史记录的数据,但我需要更多功能,例如:

- HISTORY_PROJECTION_DATE_INDEX给出了我唯一的一个日期,并且我需要用户访问此页面时的所有日期(以及小时数)

- Browser.HISTORY_PROJECTION_VISITS_INDEX返回我所做的所有访问,但我想将此数量划分为在指定时间戳发生的访问组

任何人都可以建议我如何挑选这些信息或推荐一个教程,我可以在其中找到必要的信息?预先感谢您的建议。

4

1 回答 1

0

您将需要启动内容观察器并记录发生的所有更改。我做过类似的代码。启动内容观察器并在 onChange(); 功能,阅读自上次阅读以来已更改的历史记录(您可以为此使用共享首选项)。你需要在服务中完成这一切

    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        /**
         * Get SharedPreferneces of the user
         */
        SharedPreferences pref= myContext.getSharedPreferences("com.tpf.sbrowser", 
                Context.MODE_PRIVATE);
        long wherelong = pref.getLong("Date", 0);
        DatabaseManager db=new DatabaseManager(myContext,1);
        String[] proj = new String[] { Browser.BookmarkColumns.TITLE,
                Browser.BookmarkColumns.URL, BookmarkColumns.DATE,};
        String sel = Browser.BookmarkColumns.BOOKMARK + " = 0"; 
        Cursor mCur = myContext.getContentResolver().query(
                Browser.BOOKMARKS_URI, proj, sel, null, null);
        Log.d("onChange", "cursorCount"+mCur.getCount());
        mCur.moveToFirst(); 
        String title = ""; 
        String url = ""; 
        long lastVisitedDate=0;
//You will need to create a database manager to manage your database and use its helper functions
        DbMessage msg = new DbMessage(lastVisitedDate,url, title);
        /**
         * Start reading the user history and dump into database
         */
        if(mCur.moveToFirst() && mCur.getCount() > 0) { 
              while (mCur.isAfterLast() == false) {
                  title =mCur.getString(0);
                  url = mCur.getString(1); 
                  lastVisitedDate =mCur.getLong(2); 
                  if ((lastVisitedDate>wherelong) && (!title.equals(url))) {
                      msg.set(lastVisitedDate, url, title);
                      db.InsertWithoutEnd(msg);
                      pref.edit().putBoolean("BrowserHistoryRead", true).commit();
                      pref.edit().putLong("Date", lastVisitedDate).commit();
                      myContext.updateTime(wherelong,lastVisitedDate);
                      wherelong=lastVisitedDate;
                  }
                  mCur.moveToNext(); 
              } 
          }
        mCur.close();
    }
}


/**
 * (non-Javadoc)
 * @see android.app.Service#onDestroy()
 */
@Override
public void onDestroy() {
    super.onDestroy();
    getApplication().getContentResolver().unregisterContentObserver(
            observer);
    Toast.makeText(this, "Service destroyed ...", Toast.LENGTH_LONG).show();
    state = 0;
}
}
于 2013-06-07T05:21:40.550 回答