1

Is it possible to read only the history?

By using the following code, I am able to get both history and bookmarks, but I want to read the History only.

String[] proj = new String[] { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL };
String chnHistory = Browser.BookmarkColumns.BOOKMARK + " = 0"+" OR visits > 0"; // 0 = history, 1 = bookmark
mycur = getContentResolver().query(Browser.BOOKMARKS_URI, proj, chnHistory, null, null);

int count = mycur.getCount();
mycur.moveToFirst();
array = new ArrayList<String>();
if (mycur.moveToFirst() && count > 0) 
{
  while (count > 0) 
  {
     array.add(mycur.getString(Browser.HISTORY_PROJECTION_URL_INDEX));
     count--;
     mycur.moveToNext();
  }//End of while loop
}//End of if block
if(array.size() > 0)
{       
  for (int i = 0; i < array.size(); i++) 
  {
    Log.e("result ", array.get(i));
  }
}
Glenn Teitelbaum
  • 9,339
  • 3
  • 31
  • 75
murili
  • 13
  • 1
  • 6

1 Answers1

2

To retrieve only the history, without the bookmarks, use 0.

From the Official Documentation:

Flag indicating that an item is a bookmark. A value of 1 indicates a bookmark, a value of 0 indicates a history item.

(emphasis mine)

Code:

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String[] proj = new String[] { Browser.BookmarkColumns.TITLE,
            Browser.BookmarkColumns.URL };
    String chnHistory = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history,
    Cursor mycur = this.managedQuery(Browser.BOOKMARKS_URI, proj,
            chnHistory, null, null);
    this.startManagingCursor(mycur);
    mycur.moveToFirst();

    ArrayList<String> array = new ArrayList<String>();
    String title = "";
    String url = "";

    if (mycur.moveToFirst() && mycur.getCount() > 0) {
        while (!mycur.isAfterLast()) {
            title = mycur.getString(mycur
                    .getColumnIndex(Browser.BookmarkColumns.TITLE));
            url = mycur.getString(mycur
                    .getColumnIndex(Browser.BookmarkColumns.URL));
            array.add(title + " : " + url);
            mycur.moveToNext();
        }
    }

    if (array.size() > 0) {
        for (String string : array) {
            Log.d("result ", string);
        }
    }

Add permission in Manifest file:

<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
Infinite Recursion
  • 6,315
  • 28
  • 36
  • 51