Thursday, July 25, 2013

Contd 3: How to build an Android application, step by step


Using intents to launch other applications


Initially, an application may only be launching activity classes defined within its own package. However, with the appropriate permissions, applications may also launch external activity classes in other applications.
There are well-defined intent actions for many common user tasks. For example, you can create intent actions to initiate applications such as the following:
  • Launching the built-in web browser and supplying a URL address
  • Launching the web browser and supplying a search string
  • Launching the built-in Dialer application and supplying a phone number
  • Launching the built-in Maps application and supplying a location
  • Launching Google Street View and supplying a location
  • Launching the built-in Camera application in still or video mode
  • Launching a ringtone picker

Recording a sound

Here is an example of how to create a simple intent with a predefined action (ACTION_VIEW) to launch the web browser with a specific URL:
Uri address = Uri.parse("http://www.perlgurl.org");

Intent surf = new Intent(Intent.ACTION_VIEW, address);
startActivity(surf);
This example shows an intent that has been created with an action and some data. The action, in this case, is to view something. The data is a uniform resource identifier (URI), which identifies the location of the resource to view.
For this example, the browser's activity then starts and comes into foreground, causing the original calling activity to pause in the background. When the user finishes with the browser and clicks the Back button, the original activity resumes.
Applications may also create their own intent types and allow other applications to call them, allowing for tightly integrated application suites.
The OpenIntents.org website keeps a list of intent actions atwww.openintents.org/en/intentstable. This list includes those built into Android as well as those available from third-party applications.

Working with dialogs

Handset screens are small, and user interface real estate is valuable. Sometimes you want to handle a small amount of user interaction without creating an entirely new activity. In such instances, creating an activity dialog can be very handy. Dialogs can be helpful for creating very simple user interfaces that do not necessitate an entirely new screen or activity to function. Instead, the calling activity dispatches a dialog, which can have its own layout and user interface, with buttons and input controls.

Important dialog methods of the activity class

MethodPurpose
Activity.showDialog()Shows a dialog, creating it if necessary.
Activity.onCreateDialog()Is a callback when a dialog is being created for the first time and added to the activity dialog pool.
Activity.onPrepareDialog()Is a callback for updating a dialog on-the-fly. Dialogs are created once and can be used many times by an activity. This callback enables the dialog to be updated just before it is shown for each showDialog() call.
Activity.dismissDialog()Dismisses a dialog and returns to the activity. The dialog is still available to be used again by calling showDialog() again.
Activity.removeDialog()Removes the dialog completely from the
activity dialog pool.
Activity classes can include more than one dialog, and each dialog can be created and then used multiple times.
There are quite a few types of ready-made dialog types available for use in addition to the basic dialog. These are AlertDialog, CharacterPickerDialog, DatePickerDialog, ProgressDialog, and TimePickerDialog.
You can also create an entirely custom dialog by designing an XML layout file and using the Dialog.setContentView() method. To retrieve controls from the dialog layout, you simply use the Dialog.findViewById() method.

Logging application information

Android provides a useful logging utility class called android.util.Log. Logging messages are categorized by severity (and verbosity), with errors being the most severe. The table below lists some commonly used logging methods of the Log class.

Commonly used log methods

MethodPurpose
Log.e()Logs errors
Log.w()Logs warnings
Log.i()Logs informational messages
Log.d()Logs debug messages
Log.v()Logs verbose messages
Excessive use of the Log utility can result in decreased application performance. Debug and verbose logging should be used only for development purposes and removed before application publication.
The first parameter of each Log method is a string called a tag. One common Android programming practice is to define a global static string to represent the overall application or the specific activity within the application such that log filters can be created to limit the log output to specific data.
For example, you could define a string called TAG, as follows:
private static final String TAG = "MyApp";
Now anytime you use a Log method, you supply this tag. An informational logging message might look like this:
Log.i(TAG, "In onCreate() callback method");
You can use the LogCat utility from within Eclipse to filter your log messages to the tag string.

Summary

You've seen how different Android applications can be designed using three application components: Context, Activity and Intent. Each Android application comprises one or more activities. Top-level application functionality is accessible through the application context. Each activity has a special function and (usually) its own layout, or user interface. An activity is launched when the Android system matches an intent object with the most appropriate application activity, based on the action and data information set in the intent. Intents can also be used to pass data from one activity to another.

No comments:

Post a Comment

Kindly share your view or contribution on this topic