Search code examples
androidbrowserbrowser-history

Android listen browser history last page


I get the browser history, but just want to get the information of visited pages here is my code below and the same way I want to hear the newly added phone book contacts

public class HistoryObserver extends ContentObserver {

    public final String TAG = "HistoryObserver";

    public HistoryObserver(Handler handler) {
        super(handler);
        Log.d(TAG, "Creating new HistoryObserver");``
    }

    public HistoryObserver() {
        super(null);
        Log.d(TAG, "Creating a new HistoryObserver without a Handler");
    }

    @Override
    public boolean deliverSelfNotifications() {
        Log.d(TAG, "delivering self notifications");
        return true;
    }

    @Override
    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        Log.d(TAG, "onChange without uri: " + selfChange);
        //onChange(selfChange, null);
    }

    @SuppressLint("NewApi") public void onChange(boolean selfChange, Uri uri) {
        super.onChange(selfChange, uri);
        Log.d(TAG, "onChange: " + selfChange + "\t " + uri.toString());

        String[] proj = new String[] { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL,Browser.BookmarkColumns.DATE };
        String selection = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history, 1 = bookmark
        Cursor mCursor = getContentResolver().query(Browser.BOOKMARKS_URI, proj, selection, null, null);
        //this.startManagingCursor(mCursor);
        mCursor.moveToFirst();
       int count= mCursor.getColumnCount();
      String COUNT=String.valueOf(count);
        Log.e("Browser sayac", COUNT);

        String title = "";
        String url = "";
        String date="";
        if (mCursor.moveToFirst() && mCursor.getCount() > 0) {

            while (mCursor.isAfterLast() == false) {

                title = mCursor.getString(mCursor.getColumnIndex(Browser.BookmarkColumns.TITLE ));
                url = mCursor.getString(mCursor.getColumnIndex(Browser.BookmarkColumns.URL));
                 date =  mCursor.getString(mCursor.getColumnIndex(Browser.BookmarkColumns.DATE));
                 Long timestamp = Long.parseLong(date);  

              SimpleDateFormat dateFormat = new SimpleDateFormat( "dd/MM/yyyy/HH:mm");
              Calendar calendar = Calendar.getInstance();
              calendar.setTimeInMillis(timestamp);
            String  finaldate = dateFormat.format(calendar.getTime());
              String smsDate = finaldate.toString();

                // Log.e("DTE", date);
                    Log.e("URL",title  );

                    Log.e("TARIH", smsDate);
                mCursor.moveToNext();
            }
        }
    }
}

Solution

  • change it to

    //add thread.sleep to delay the action because sometimes it takes around a second before the page history is added. you can use other delaying techniques though
    try {
        Thread.sleep(1500);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    
    Cursor mCursor = getContentResolver().query(Browser.BOOKMARKS_URI, proj, selection, null, null);
    //this.startManagingCursor(mCursor);
    mCursor.moveToFirst(); // instead of movetolast