问题
Hi friends i am new to android I don't know how to retrieve and display history in web browser.
Please guide me and share your thoughts, ideas and bookmarks too.
I have to display history in ListView
回答1:
ArrayList mTitles=new ArrayList();
ArrayList mUrls=new ArrayList();
public void getBrowserHist() {
Cursor mCur = managedQuery(Browser.BOOKMARKS_URI,
Browser.HISTORY_PROJECTION, null, null, null);
mCur.moveToFirst();
if (mCur.moveToFirst() && mCur.getCount() > 0) {
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();
}
}
}
Add below uses-permission into your manifest file.
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
Enjoy :-)
回答2:
You can use getContentResolver
to get the browser history as managedQuery
has been depricated.
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();
String title = "";
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 in your manifest
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
For more details to access data using Cursor
check Access Data using Cursor
来源:https://stackoverflow.com/questions/24277592/how-to-retrieve-and-display-browser-history-and-bookmarks-in-browser-application