android - 在后台打开浏览器并在 Android 中读取浏览器 URL 文本

标签 android browser

我正在制作一个演示应用程序,其中我想在单击按钮时在后台打开浏览器,并且在打开浏览器后必须读取浏览器 URL 栏文本。我怎样才能达到同样的效果。是否可行。我已经找到这个来阅读网址,但是我不清楚如何在后台打开浏览器。

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

谢谢

最佳答案

尝试

 CookieStore cookieStore = new BasicCookieStore();

 HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
  localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
  HttpClient http = new DefaultHttpClient();
 HttpPost post = new HttpPost(url);
 HttpResponse response = null;
try {
    response = http.execute(post,localContext);
} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
String str=(String) localContext.getAttribute("last_redirect_url");
               if (str == null)
                {
                   HttpUriRequest currentReq = (HttpUriRequest) localContext.getAttribute(ExecutionContext.HTTP_REQUEST);
                   HttpHost currentHost = (HttpHost)  localContext.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
                   str = (currentReq.getURI().isAbsolute()) ? currentReq.getURI().toString() : (currentHost.toURI() + currentReq.getURI());
                  System.out.print(str); 
               } 
}

关于android - 在后台打开浏览器并在 Android 中读取浏览器 URL 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16140748/

相关文章:

android - 我正在从我的 API 取回一个 token 并可以在 Logcat 中打印它我需要保存该响应但我没有任何运气。我是 Kotlin 的新手

android - 从 priv-app 应用程序运行 shell 命令

android - 设备检测是否是android?

Android Studio 编译原生代码显示 "fcntl(): Bad file descriptor"

python - 防止 PyCharm 在运行时打开浏览器/新选项卡

javascript - Web 浏览器是否应该允许以编程方式清除(过期)缓存?

http - url 重写是如何工作的?

android - 如何修改此 SQLiteOpenHelper 插入操作,使其成为 ContentResolver 操作(带事务)

javascript - 为了让浏览器知道始终使用缓存,我应该设置什么响应 header ?

browser - 显示网络浏览器设置