我正在尝试从Android浏览器获取历史记录和搜索结果。 在下面的代码中,我得到了所有书签,它们很有用:
public void getBrowser(){
String[] requestedColumns = {
Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.VISITS,
Browser.BookmarkColumns.BOOKMARK
};
Cursor faves = managedQuery(Browser.BOOKMARKS_URI, requestedColumns,
Browser.BookmarkColumns.BOOKMARK + "=1", null, Browser.BookmarkColumns.VISITS);
Log.d(DEBUG_TAG, "Bookmarks count: " + faves.getCount());
int titleIdx = faves.getColumnIndex(Browser.BookmarkColumns.TITLE);
int visitsIdx = faves.getColumnIndex(Browser.BookmarkColumns.VISITS);
int bmIdx = faves.getColumnIndex(Browser.BookmarkColumns.BOOKMARK);
faves.moveToFirst();
while (!faves.isAfterLast()) {
Log.d("SimpleBookmarks", faves.getString(titleIdx) + " visited " + faves.getInt(visitsIdx) + " times : " + (faves.getInt(bmIdx) != 0 ? "true" : "false"));
faves.moveToNext();
}
}
当我尝试仅从浏览器获取历史记录时,我正在尝试使用以下代码:
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();
}
}
}
问题是我现在获得了所有书签,历史和访问量最高的页面。我只想要历史专栏。我也不是谷歌搜索的搜索结果。我已经尝试过SEARCHES_URI对象,但我无法让它工作。 有没有人有任何建议我可以解决我的问题?
答案 0 :(得分:10)
出于某种奇怪的原因,谷歌决定将书签与历史混合在一起,称其为SDK中的“书签”。 尝试以下代码,重要的是按“书签”类型进行过滤。
String[] proj = new String[] { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL };
String sel = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history, 1 = bookmark
mCur = this.managedQuery(Browser.BOOKMARKS_URI, proj, sel, null, null);
this.startManagingCursor(mCur);
mCur.moveToFirst();
String title = "";
String url = "";
if (mCur.moveToFirst() && mCur.getCount() > 0) {
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();
}
}
答案 1 :(得分:1)
试试这个:
package higherpass.TestingData;
import android.app.Activity;
import android.os.Bundle;
import android.provider.Browser;
import android.widget.TextView;
import android.database.Cursor;
public class TestingData extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView view = (TextView) findViewById(R.id.hello);
String[] projection = new String[] {
Browser.BookmarkColumns.TITLE
, Browser.BookmarkColumns.URL
};
Cursor mCur = managedQuery(android.provider.Browser.BOOKMARKS_URI,
projection, null, null, null
);
mCur.moveToFirst();
int titleIdx = mCur.getColumnIndex(Browser.BookmarkColumns.TITLE);
int urlIdx = mCur.getColumnIndex(Browser.BookmarkColumns.URL);
while (mCur.isAfterLast() == false) {
view.append("n" + mCur.getString(titleIdx));
view.append("n" + mCur.getString(urlIdx));
mCur.moveToNext();
}
}
}
摘自here