Android - 访问浏览器历史记录

时间:2013-05-26 21:33:34

标签: android browser-history android-browser

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

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会返回我所做的所有访问,但我想将此金额除以在指定时间戳发生的访问次数

有人可以建议我如何剔除这些信息或推荐一个可以找到必要信息的教程?提前感谢您的建议。

1 个答案:

答案 0 :(得分: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;
}
}