Gaurav Arora
Gaurav Arora

Reputation: 8362

Open Browser in Background and read the Browser URL Text in Android

I am making a demo application, in which I want to open the browser in the background on a button click and have to read the browser URL bar text after the browser is opened. How can I achieve the same. Is it feasible. I have found out this to read the URL , but how to open the browser in the background is not clear to me.

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

Thanks

Upvotes: 1

Views: 1385

Answers (1)

Akilan
Akilan

Reputation: 934

try

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

Upvotes: 1

Related Questions