在android中获取浏览器历史记录和搜索结果

发布于 2024-12-07 15:12:05 字数 1625 浏览 0 评论 0原文

我正在尝试从 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 对象,但无法让它工作。 有人有什么建议可以解决我的问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蝶…霜飞 2024-12-14 15:12:05

由于某些奇怪的原因,Google 决定将书签和历史记录混合在一起,在 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();
        }
    }

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();
        }
    }
为你拒绝所有暧昧 2024-12-14 15:12:05

试试这个:

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

    }
}

此处提取

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文