Get browser history and search result in android

一个人想着一个人 提交于 2019-12-28 12:50:47

问题


I am trying to get the history and search results from the android browser. In the following code I get all the bookmarks, which works great:

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();
    }
}

When I am trying to only get the history from the browser I am trying following code:

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();
        }
    }
}

The problem is that I now get all the bookmarks, history and top visited pages. And I only want the history columns. I also wan´t the search results from google search. I have tried the SEARCHES_URI object but I can´t get it to work. Does anyone have any suggestion hove I can solve my problem?


回答1:


For some strange reason, Google decided to mix bookmarks and history calling them "Bookmarks" in the SDK. Try the following code, the important thing is to filter by "bookmark" type.

    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();
        }
    }



回答2:


Try this:

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();
        }

    }
}

extracted from here



来源:https://stackoverflow.com/questions/7626084/get-browser-history-and-search-result-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!