Question

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.

Was it helpful?

Solution

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)

OTHER TIPS

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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top