在Android浏览器中获取当前URL [英] Getting the current URL in an Android browser

查看:508
本文介绍了在Android浏览器中获取当前URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法来获取用户正在android浏览器应用程序上访问的当前URL。
我发现可以使用以下技术从 Browser.BOOKMARKS_URI 数据库获取上次访问的URL:

I'm searching for a way to get the current URL a user is visiting on the android browser application. I figured out that I can get the last visited URL from the Browser.BOOKMARKS_URI database using the following technique:

Cursor cursor = context.getContentResolver().query(Browser.BOOKMARKS_URI,
        Browser.HISTORY_PROJECTION, null, null,
        Browser.BookmarkColumns.DATE + " DESC");
cursor.moveToNext();
String url = cursor.getString(Browser.HISTORY_PROJECTION_URL_INDEX);
cursor.close();

问题在于,这是 Browser.BOOKMARKS_URI db不会更新,查询返回错误的结果。

The problem with this, is that the Browser.BOOKMARKS_URI db doesn't get updated when the user presses back in order to navigate to the previous page in the browser, and the query returns incorrect results.

请参阅以下示例:


  1. 用户导航至www.google.com-> 查询返回 www.google.com

  2. 用户导航到www.imdb.com-> 查询返回 www.imdb.com

  3. 用户按下返回返回www.google.com-> 查询返回 www.imdb.com(!!)

  1. user navigates to www.google.com -> Query returns "www.google.com"
  2. user navigates to www.imdb.com -> Query returns "www.imdb.com"
  3. user presses back to return to www.google.com -> Query returns "www.imdb.com" (!!)

有人知道如何返回用户正在查看的正确网址吗?

Does anyone have any idea how to return the correct url a user is viewing?

推荐答案

我认为您做到了还没有通过以下方法。一旦尝试!
您可以像对其他ContentProviders一样访问浏览历史记录。除了浏览历史记录,您还可以获取书签列表。

I think you did not have gone through the following approah. once try it! You can access Browsing history the same way you do that for other ContentProviders. Besides browsing history you can also get list of Bookmarks.

Cursor webLinksCursor = getContentResolver().query(Browser.BOOKMARKS_URI, Browser.HISTORY_PROJECTION, null, null, Browser.BookmarkColumns.DATE + " DESC");
int row_count = webLinksCursor.getCount();

int title_column_index = webLinksCursor.getColumnIndexOrThrow(Browser.BookmarkColumns.TITLE);
int url_column_index = webLinksCursor.getColumnIndexOrThrow(Browser.BookmarkColumns.URL);

if ((title_column_index > -1) && (url_column_index > -1) && (row_count > 0))
{
    webLinksCursor.moveToFirst();
    while (webLinksCursor.isAfterLast() == false)
    {
        if (webLinksCursor.getInt(Browser.HISTORY_PROJECTION_BOOKMARK_INDEX) != 1)
        {
            if (!webLinksCursor.isNull(url_column_index))
            {
                Log.i("History" , "Last page browsed " + webLinksCursor.getString(url_column_index));
                break;
            }
        }
        webLinksCursor.moveToNext();
    }            
}
webLinksCursor.close();

并且您还需要获得许可

com.android.browser.permission.READ_HISTORY_BOOKMARKS

这篇关于在Android浏览器中获取当前URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆