0

I'm trying to develop an android app that could delete android's built in browser bookmark. Here is my code

ContentResolver cr = getContentResolver();
try 
{
    Cursor c = cr.query(
                        Browser.BOOKMARKS_URI,
                        new String [] { Browser.BookmarkColumns._ID,
                                        Browser.BookmarkColumns.BOOKMARK,
                                        Browser.BookmarkColumns.VISITS },
                        "bookmark != 0",
                        null,
                        null);
    c.moveToFirst();
    cr.delete(Browser.BOOKMARKS_URI, null, null);

} 
catch (IllegalStateException e) 
{
    e.printStackTrace();
}

The problem with the above code is that it is able to delete the bookmark perfectly fine. It deletes Bookmark but it will also deletes the Browser History as well which it's not supposed to. Please help me to clear this riddle, Thanks in advance.

Chethan Shetty
  • 1,972
  • 4
  • 25
  • 45

2 Answers2

2

My guess is that by calling

cr.delete(Browser.BOOKMARKS_URI, null, null);

You request to delete everything from the Bookmark's table (since you pass null in the "where" param) According to Android doc, the BOOKMARKS_URI is :

A table containing both bookmarks and history items

So... you should be more specific regarding what you want to delete (add bookmark == 1 to the cr.delete(...) call or something)

dors
  • 5,802
  • 8
  • 45
  • 71
  • I didn't get you @dors can you explain what's meant by `you should be more specific regarding what you want to delete (add bookmark == 1 to the cr.delete(...) call or something)` according to you ? – Chethan Shetty Jul 04 '13 at 07:38
  • 1
    cr.delete(Uri url, String where, String[] selectionArgs) has 3 params. In the first param you passed 'Browser.BOOKMARKS_URI", which means "I want to delete stuff from the Browser.BOOKMARKS_URI table". As I mentioned in my answer, Browser.BOOKMARKS_URI is a Database table that inbludes both Bookmarks AND history items. In the second and third params you passed "null", which means "there is no restriction as to what I want to delete, delete everything from Browser.BOOKMARKS_URI". So try to pass "bookmark == 1" in the second param (the "where" param) to specify you only want to delete bookmarks. – dors Jul 04 '13 at 07:59
  • @ChethanShetty for deletion of history what i need to pass in where parameter – Erum Feb 11 '15 at 10:34
2

Here is the working code, credits to @dors

ContentResolver cr = getContentResolver();
try 
{
Cursor c = cr.query(
                    Browser.BOOKMARKS_URI,
                    new String [] { Browser.BookmarkColumns._ID,
                                    Browser.BookmarkColumns.BOOKMARK,
                                    Browser.BookmarkColumns.VISITS },
                    "bookmark != 0",
                    null,
                    null);
c.moveToFirst();
cr.delete(Browser.BOOKMARKS_URI, "bookmark == 1", null);

} 
catch (IllegalStateException e) 
{
    e.printStackTrace();
}
Chethan Shetty
  • 1,972
  • 4
  • 25
  • 45