This quick tip shows you how to launch the built-in Browser application in three ways. First, you learn how to launch the browser to a specific URL. Second, you learn how to create text with links. Third, you learn how to launch a Google web search and specify the search criteria. You will achieve these goals by creating and configuring the appropriate Intents within your application’s Activity class.
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.
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:
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:
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:
startActivity(launchBrowser);
When you click on this button, the Browser application (which generally handles HTML content display) is launched to the website you provided.