Search code examples
androidandroid-intentbookmarks

Android Create browser bookmark


Possible Duplicate:
Create Browser-Bookmark from app

I have tried to create a bookmark for the android web browser using the following code.

public static final void saveBookmark(Context c, String title, String url) {
    Intent i = new Intent(Intent.ACTION_INSERT,
                  android.provider.Browser.BOOKMARKS_URI);
    i.putExtra("title", title);
    i.putExtra("url", url);
    c.startActivity(i);
}

The problem is that the code doesn't directly save the bookmark, but alerts the user with a screen where they can cancel or save the bookmark. How can I create a bookmark without asking the user with an "alert screen"?


Solution

  • android.provider.Browser.saveBookmark(c,title,url); is the default way to do this, but it probably will show the alert if the user has those alerts enabled.

    The Android framework doesn't have direct API calls to circumvent this. However, the bookmarks of the browser are available using the content provider at content uri: android.provider.Browser.BOOKMARKS_URI. Which you can manipulate with queries, including adding bookmarks to it.

    To make this work you need to have the com.android.browser.permission.READ_HISTORY_BOOKMARKS and com.android.browser.permission.WRITE_HISTORY_BOOKMARKS permissions.

    If you are unfamiliar with content providers, you can read a introduction to them here: http://developer.android.com/guide/topics/providers/content-provider-basics.html