从应用程序创建浏览器书签
Posted
技术标签:
【中文标题】从应用程序创建浏览器书签【英文标题】:Create Browser-Bookmark from app 【发布时间】:2011-06-03 02:37:04 【问题描述】:我想从我自己的应用程序中的股票 android 浏览器中创建一个书签。我该怎么做?
我只找到了Browser.saveBookmark
-Method (api-doc),但这会显示一个新窗口,用户可以在其中更改数据。因为我想从外部数据源导入书签,所以我想直接保存书签而不要求用户输入。
【问题讨论】:
【参考方案1】:我从 Android 实现中获取了以下代码(并禁用了“添加书签”toast):
/**
* Add a bookmark to the database.
*
* @param context
* Context of the calling Activity. This is used to make Toast confirming that the bookmark has been added. If the caller provides null, the Toast will not be shown.
* @param cr
* The ContentResolver being used to add the bookmark to the db.
* @param url
* URL of the website to be bookmarked.
* @param name
* Provided name for the bookmark.
* @param thumbnail
* A thumbnail for the bookmark.
* @param retainIcon
* Whether to retain the page's icon in the icon database. This will usually be <code>true</code> except when bookmarks are added by a settings restore agent.
*/
static void addBookmark(Context context, ContentResolver cr, String url, String name, Bitmap thumbnail, boolean retainIcon)
final String WHERE_CLAUSE = "url = ? OR url = ? OR url = ? OR url = ?";
final String WHERE_CLAUSE_SECURE = "url = ? OR url = ?";
String[] SELECTION_ARGS;
// Want to append to the beginning of the list
long creationTime = new Date().getTime();
// First we check to see if the user has already visited this
// site. They may have bookmarked it in a different way from
// how it's stored in the database, so allow different combos
// to map to the same url.
boolean secure = false;
String compareString = url;
if (compareString.startsWith("http://"))
compareString = compareString.substring(7);
else if (compareString.startsWith("https://"))
compareString = compareString.substring(8);
secure = true;
if (compareString.startsWith("www."))
compareString = compareString.substring(4);
if (secure)
SELECTION_ARGS = new String[2];
SELECTION_ARGS[0] = "https://" + compareString;
SELECTION_ARGS[1] = "https://www." + compareString;
else
SELECTION_ARGS = new String[4];
SELECTION_ARGS[0] = compareString;
SELECTION_ARGS[1] = "www." + compareString;
SELECTION_ARGS[2] = "http://" + compareString;
SELECTION_ARGS[3] = "http://" + SELECTION_ARGS[1];
Cursor cursor = cr.query(Browser.BOOKMARKS_URI, Browser.HISTORY_PROJECTION, secure ? WHERE_CLAUSE_SECURE : WHERE_CLAUSE, SELECTION_ARGS, null);
ContentValues map = new ContentValues();
if (cursor.moveToFirst() && cursor.getInt(Browser.HISTORY_PROJECTION_BOOKMARK_INDEX) == 0)
// This means we have been to this site but not bookmarked
// it, so convert the history item to a bookmark
map.put(Browser.BookmarkColumns.CREATED, creationTime);
map.put(Browser.BookmarkColumns.TITLE, name);
map.put(Browser.BookmarkColumns.BOOKMARK, 1);
// map.put(Browser.BookmarkColumns.THUMBNAIL, bitmapToBytes(thumbnail));
cr.update(Browser.BOOKMARKS_URI, map, "_id = " + cursor.getInt(0), null);
else
int count = cursor.getCount();
boolean matchedTitle = false;
for (int i = 0; i < count; i++)
// One or more bookmarks already exist for this site.
// Check the names of each
cursor.moveToPosition(i);
if (cursor.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX).equals(name))
// The old bookmark has the same name.
// Update its creation time.
map.put(Browser.BookmarkColumns.CREATED, creationTime);
cr.update(Browser.BOOKMARKS_URI, map, "_id = " + cursor.getInt(0), null);
matchedTitle = true;
break;
if (!matchedTitle)
// Adding a bookmark for a site the user has visited,
// or a new bookmark (with a different name) for a site
// the user has visited
map.put(Browser.BookmarkColumns.TITLE, name);
map.put(Browser.BookmarkColumns.URL, url);
map.put(Browser.BookmarkColumns.CREATED, creationTime);
map.put(Browser.BookmarkColumns.BOOKMARK, 1);
map.put(Browser.BookmarkColumns.DATE, 0);
// map.put(Browser.BookmarkColumns.THUMBNAIL, bitmapToBytes(thumbnail));
int visits = 0;
if (count > 0)
// The user has already bookmarked, and possibly
// visited this site. However, they are creating
// a new bookmark with the same url but a different
// name. The new bookmark should have the same
// number of visits as the already created bookmark.
visits = cursor.getInt(Browser.HISTORY_PROJECTION_VISITS_INDEX);
// Bookmark starts with 3 extra visits so that it will
// bubble up in the most visited and goto search box
map.put(Browser.BookmarkColumns.VISITS, visits + 3);
cr.insert(Browser.BOOKMARKS_URI, map);
if (retainIcon)
WebIconDatabase.getInstance().retainIconForPageUrl(url);
cursor.close();
【讨论】:
我正在寻找保存书签的答案,我想我找到了答案。但我确实有一个问题,只是为了向您解释我想要实现的目标。我创建了一个类似于 google 的 webview,用户可以在那里搜索 url 和东西,我正在尝试使用 google 之类的书签来保存链接。你认为你的答案就足够了,还是我需要为此写一个数据库?我有一个 recyclerview 列表,用户将在那里看到所有已保存的书签。 那是 4 年前的事了,我不再为 android 开发 - 抱歉,我不记得了...【参考方案2】:如果您只想允许用户添加书签,android.provider.Browser.saveBookmark() 就是您想要的。但是看起来您想要进行批量更新,所以这可能还不够,因为它只是打开了浏览器的书签页面。
AFAIK 没有直接绑定到浏览器书签的开放 API。但是,它有一个内容解析器,可以访问 android.provider.Browser.BOOKMARKS_URI。解决提供程序后,您可以通过运行查询来操作书签,前提是您具有 com.android.browser.permission.READ_HISTORY_BOOKMARKS 和 com.android.browser.permission.WRITE_HISTORY_BOOKMARKS 权限。
如果您不熟悉内容提供者,他们可能会有点麻烦(如果您不熟悉 SQL,情况会更糟)。不过,the knowledge base 有一些关于它们的好文章,快速 google 一下“android 内容提供者教程”应该会让你顺利上路。
【讨论】:
另外,浏览器本身的代码可以在这里阅读:android.git.kernel.org/?p=platform/packages/apps/…。我建议任何想做严肃的 android 工作的人浏览操作系统代码以了解他们将与之交互的东西。 你能帮帮我吗? ***.com/questions/13725192/… 您具体需要什么帮助?你检查过链接吗? James,我想知道如何在 Android 上获取/设置每个帐户的书签。请参阅我的问题***.com/questions/13725192/… 了解更多详情。 我不确定默认浏览器是如何做到的,但谷歌的源代码是免费提供的。 grepcode.com/file/repository.grepcode.com/java/ext/…以上是关于从应用程序创建浏览器书签的主要内容,如果未能解决你的问题,请参考以下文章
javascript [一键订阅Inoreader书签脚本]可用于在浏览器创建一个“一键订阅至Inoreader”的脚本书签按钮