Android 读取浏览器历史记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2577084/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android read browser history
提问by Mitul Nakum
I want to read browser history in Android phone.
我想在 Android 手机中阅读浏览器历史记录。
I have done some document reading, then I come to know that we can read browser history by android.provider.Browser class. It has :
我做了一些文档阅读,然后我知道我们可以通过 android.provider.Browser 类读取浏览器历史记录。它有:
final static Cursor
getAllVisitedUrls(ContentResolver cr)
...method which returns Cursor
.
...返回的方法Cursor
。
May I get help to handle Cursor, or any example code to get browser history?
我可以得到处理 Cursor 的帮助,或者获取浏览器历史记录的任何示例代码吗?
采纳答案by Macarse
Not really an answer but I can tell you what I did.
不是真正的答案,但我可以告诉你我做了什么。
I first clone the browser repoand try to reproduce how they get the history. And I started getting:
我首先克隆浏览器存储库并尝试重现它们如何获取历史记录。我开始得到:
Permission Denial: reading com.android.browser.BrowserProvider
权限拒绝:读取 com.android.browser.BrowserProvider
So I added:
所以我补充说:
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
But it still is giving me the same error. I google it and I found this Accessing Data With Android Cursors.
但它仍然给我同样的错误。我用谷歌搜索,发现这个Accessing Data With Android Cursors。
Hope it helps.
希望能帮助到你。
回答by Aditya
managedQuery has been deprecated so use getContentResolver instead, use the following code:
managedQuery 已被弃用,因此请改用 getContentResolver,使用以下代码:
String[] proj = new String[] { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL };
String sel = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history, 1 = bookmark
Cursor mCur = getContentResolver().query(Browser.BOOKMARKS_URI, proj, sel, null, null);
mCur.moveToFirst();
@SuppressWarnings("unused")
String title = "";
@SuppressWarnings("unused")
String url = "";
if (mCur.moveToFirst() && mCur.getCount() > 0) {
boolean cont = true;
while (mCur.isAfterLast() == false && cont) {
title = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.TITLE));
url = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.URL));
// Do something with title and url
mCur.moveToNext();
}
}
Also add permissions using
还使用添加权限
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
回答by Mitul Nakum
For Lollipop or earlier
棒棒糖或更早版本
I am able to get the history by using the following code:
我可以使用以下代码获取历史记录:
Cursor mCur = activity.managedQuery(Browser.BOOKMARKS_URI,
Browser.HISTORY_PROJECTION, null, null, null);
if (mCur.moveToFirst()) {
while (mCur.isAfterLast() == false) {
Log.v("titleIdx", mCur
.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX));
Log.v("urlIdx", mCur
.getString(Browser.HISTORY_PROJECTION_URL_INDEX));
mCur.moveToNext();
}
}
回答by sromku
This post is a little bit old, but here is another easy solution for getting data related to Bookmark
and Search
content providers in Android:
这篇文章有点旧,但这里有另一个在 Android 中获取Bookmark
与Search
内容提供者相关的数据的简单解决方案:
Use this lib: https://github.com/EverythingMe/easy-content-providers
使用这个库:https: //github.com/EverythingMe/easy-content-providers
Get all bookmarks:
获取所有书签:
BrowserProvider browserProvider = new BrowserProvider(context);
List<Bookmark> bookmarks = browserProvider.getBookmarks().getList();
Each Bookmarkhas all fields, so you can get any info you need: title, url, visits, ...
每个书签都有所有字段,因此您可以获得所需的任何信息: 标题、网址、访问次数……
Get all Searchhistory:
获取所有搜索历史:
List<Search> searches = browserProvider.getSearches().getList();
It works with lists or cursor and there a sample app to see how it looks and works.
它适用于列表或光标,还有一个示例应用程序可以查看它的外观和工作方式。
In fact, there is support for all Android content providers like: Contacts, SMS, Calls, ...Full doc with all options: https://github.com/EverythingMe/easy-content-providers/wiki/Android-providers
实际上,支持所有 Android 内容提供程序,例如:联系人、短信、电话……包含所有选项的完整文档:https: //github.com/EverythingMe/easy-content-providers/wiki/Android-providers
Hope it helped :)
希望它有所帮助:)
回答by naidu
public ArrayList<HistoryEntry> getBrowserHistory() {
String title = "";
String url = "";
ArrayList<HistoryEntry> list = new ArrayList<HistoryEntry>();
String[] proj = new String[] { Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.URL };
String sel = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history,
// 1 = bookmark
Cursor mCur = getContentResolver().query(Browser.BOOKMARKS_URI, proj,
sel, null, null);
mCur.moveToFirst();
if (mCur.moveToFirst() && mCur.getCount() > 0) {
boolean cont = true;
while (mCur.isAfterLast() == false && cont) {
HistoryEntry entry = new HistoryEntry();
title = mCur.getString(mCur
.getColumnIndex(Browser.BookmarkColumns.TITLE));
url = mCur.getString(mCur
.getColumnIndex(Browser.BookmarkColumns.URL));
// Do something with title and url
entry.setTitle(title);
entry.setUrl(url);
list.add(entry );
Log.d("TAG", "title " + title);
mCur.moveToNext();
}
}
mCur.close();
return list;
}