Taste of Android :: Part-5
Bhaskar S | 05/19/2013 |
Android Application Development - IV
In Part-4 we used Intent to trigger an event in the Android system to navigate between activities within our application.
Interestingly, Intents can also be used to launch built-in applications that come with the Android device. This is a very powerful and useful concept.
Before we demonstrate the ability to launch built-in applications, let us understand the basics of an Intent.
An Intent is a passive object that encapsulates some information which the Android system can use to determine which component in the Android system should handle the Intent. Once the appropriate component is identified, that component will then act accordingly based on the information in the Intent.
The following are the information parts in an Intent:
Component :: name of the component that should handle the Intent. It is specified as two parts - the full class name of the component such as com.android.calculator2.Calculator and the package name as specified in the AndroidManifest.xml file where the component is defined such as com.android.calculator2.
Component name is optional
Action :: a string that names the action to be performed. The Android framework class Intent has a number of pre-defined actions such as Intent.ACTION_VIEW which is used to display data to the user, etc. We can also defined our own custom action as we did in Part-4 to launch the quiz activity.
Action name is a required
Data :: the URI of the data to be acted by the intended component as well as the data type (or scheme type) of that data. For example, launch the built-in browser application to open an URL to open in the browser. The specified URL is the data that the browser application needs to act on and http is the scheme type.
Action data is optional
Category :: a string that provides additional information on the type of component that should handle the Intent such as Intent.CATEGORY_LAUNCHER which indicates the initial activity of an application that is listed in the top-level launcher.
Category is optional
Let us create a new Android application named DroidAppLauncher to illustrate the concept of launching built-in Android applications using an Intents. This application will display a screen with four buttons - one to launch the browser app, second to launch the calculator app, third to launch the calendar app, and the last one to exit the application. We will not go step-by-step to show the various screens since we already did that in Part-2 for the DroidTipCalculator application.
Modify the contents of the dimens.xml file to look like the one shown in the listing 5.1 below:
Next, modify the contents of the strings.xml file to look like the one shown in the listing 5.2 below:
The contents of the activity_launcher.xml layout file will look like the one shown in the listing 5.3 below:
Previewing the activity_launcher.xml layout file in the Graphical Mode in Eclipse will look like the one shown in the figure 5.1 below:
The contents of the java source file LauncherActivity.java will look like the one shown in the listing 5.4 below:
When the DroidAppLauncher application starts up, the user is presented with the main launcher screen as shown in the following figure 5.2 below:
When the user clicks on the Launch Browser button, we create an instance of the Intent object. Next, we set the action to be the pre-defined generic Android action Intent.ACTION_VIEW by calling the method setAction(). Since the action indicates we desire to view some data, we need to provide some data on what we want to view. For this, we set the URI data as the URL of the Google home page http://www.google.com by calling the method setData(). Finally, we send our Intent to teh Android system by calling the method startActivity(). Android system will the launch the built-in browser application as shown in the following figure 5.3 below:
To exit the Browser application and go back to the main launcher screen, click on the BACK button as shown in the following figure 5.4 below:
When the user clicks on the Launch Calculator button, we create an instance of the Intent object. Next, we set the action to be the pre-defined generic Android action Intent.ACTION_MAIN by calling the method setAction(). This action indicates we desire to start the main screen of the application. Next, we set the category to be the pre-defined generic Android category Intent.CATEGORY_LAUNCHER by calling the method setCategory(). Next, we set the component name (class and package) of the Calculator application by calling the method setComponent(). Finally, we send our Intent to the Android system by calling the method startActivity(). Android system will the launch the built-in calculator application as shown in the following figure 5.5 below:
To exit the Calculator application and go back to the main launcher screen, click on the BACK button.
When the user clicks on the Launch Calendar button, we perform similar steps as we did to launch the Calculator application. Android system will the launch the built-in calendar application as shown in the following figure 5.6 below:
To exit the Calendar application and go back to the main launcher screen, click on the BACK button.
Finally, when the user clicks on the Exit button, we exit the DroidAppLauncher application.
References