Via : OpenAndroid.in
Tags : #Downloads, #Source_Code, #Tips__Tricks, #Tutorial
Orginal Post: How to open Web Browser with given http link

Opening a webpage In Browser



We can open a Web Page with Given URL in Android.  To do this start an Activity passing the URL of the WebPage as data.
Make sure you declare following permission to access Internet
<uses-permission android:name="android.permission.INTERNET" /> 

 Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse("http://www.indianrail.gov.in/pnr_Enq.html"));
startActivity(i);


Step 1: Create an Android Application


Begin by creating an Android project. Implement your Android application as normal. Once you have a project set up and the application running, decide under what circumstances you want to launch the browser. Will this occur when Button controls are pressed? Implement the necessary controls that will trigger to web browsing or searching features of the application, including any click handling. Once you have completed these tasks, you have places to drop in the code to launch the browser or web search. Now you are ready to proceed with this quick tip.

Download: HelloWorldWideWeb, which is available as open source.

Step 2: Working with URIs


Android uses Uri (Uniform Resource Identifier) objects to identify the unique location of a piece of data. Uri objects are often used to specify the data that an Intent is supposed to use. In this case, we will create a Uri object from a web URL using the parse() method:

  1. Uri uriUrl = Uri.parse("http://androidbook.blogspot.com/");


Step 3: Creating the Intent


You can view HTML content using the following Intent: android.content.Intent.ACTION_VIEW. Begin by creating an Intent of this type and specifying the URI you created above, as follows, within your Button click handler:

  1. Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);


Step 4: Launching the Intent


When you launch this Intent, any applications that can display web will be able to handle this request. Once you have set the type data for the Intent, you can call the startActivity() method, passing in your Intent:

  1. startActivity(launchBrowser);


When you click on this button, the Browser application (which generally handles HTML content display) is launched to the website you provided.
Android WebView

When you hit the back button, you return to the previous Activity, which happens to be your application.

Step 5: Using Links in Text


Another easy way to launch in to the browser is simply by including links within text on the screen. The TextView object can be configured to find these and turn then in to clickable links, like in a web browser, such that when the user clicks on them they launch the browser to the appropriate spot. For instance, the following TextView does just that:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/contains_links"
android:textSize="14dp"
android:autoLink="web" />

The following screenshot shows what this looks like.
Open Android Browser

The text for @string/contains_links is verbatim for what you see on the screen. No special formatting commands or tags are needed within the string.

Step 6: Enabling Web Searches


When you want to provide the user with the ability to perform a web search, you could still use the ACTION_VIEW intent and set up the query strings appropriate to a specific search engine, or if you are content with a Google search, you can simply use the web search Intent: android.content.Intent.ACTION_WEB_SEARCH. Begin by creating an Intent of this type, as follows, within your second Button click handler:
Intent search = new Intent(Intent.ACTION_WEB_SEARCH);

Step 7: Supplying Search Criteria


Often, you want to supply some criteria to search on. You can do this by supplying this information as part of the Intent’s extras. The ACTION_WEB_SEARCH Intent specifically uses the SearchManager.QUERY extra field for the search criteria. For example, to perform the Google search on pygmy goats, you configure the SearchManager.QUERY extra and launch the Browser as follows:
Intent search = new Intent(Intent.ACTION_WEB_SEARCH);
search.putExtra(SearchManager.QUERY, "pygmy goats");
startActivity(search);

When you click on this button, the Browser application (which generally handles HTML content display) is launched to the website you provided.
Android WebView

A Note on Permissions: Although your application is leveraging browser capabilities on the device, it is not required to have any such permissions. This is because the application is not directly displaying web content. Instead, it’s just leveraging other applications’ capabilities to do so.

Via : OpenAndroid.in
Tags : #Downloads, #Source_Code, #Tips__Tricks, #Tutorial
Orginal Post: How to open Web Browser with given http link

0 comments:

Post a Comment

 
Top