3

I'm writing an application, which have to help me get all information about browser history, so I wrote a simple code:

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


    }
}

Method takeHistory() helps me to take some data about browser history, but I need more functionality, like:

- HISTORY_PROJECTION_DATE_INDEX gives my only one date, and I need all dates (and also hours) when the user visited this page

- Browser.HISTORY_PROJECTION_VISITS_INDEX returns all visits which I made, but I want to divide this amount into gruops of visits which took place at the specified timestamp

Can anybody suggest how can I cull this information or recommend a tutorial, in which I can find necessary information? Thank you in advance for your advice.

Ziva
  • 3,181
  • 15
  • 48
  • 80

1 Answers1

0

You will need to start content observor and record all the changes that occur. I have done similar code. Start a content observor and in the onChange(); function, read the history that has changed since last time you read it(you can use shared preferences for that). And you need to do this all in a service

    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;
}
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Aditya
  • 5,509
  • 4
  • 31
  • 51
  • Thanks for the sample. since you are register/un-register it in Activity/Service context therefore, you will not be notified when the application is not running. I'm looking for a way to be notified even when the app is not running. please have a look at: http://stackoverflow.com/q/21542098/513413 thanks. – Hesam Feb 04 '14 at 02:59