Android Development Basics

Thank goodness you don’t have to be a member of Mensa to develop Android applications! Developing in Android is simple because its default language is Java. Though writing Android applications is fairly easy, developing alone can be a difficult task to conquer.

tip.eps If you’ve never developed applications before, this book isn’t the best place to start reading about app development. Pick up a copy of Beginning Programming with Java For Dummies, by Barry Burd (John Wiley & Sons, Inc.) to learn the ropes. After you have a basic understanding of Java under your belt, you should be ready to tackle this book.

Although the Android operating system consists primarily of Java code, small parts of the framework aren’t included. Android uses the XML language as well as basic Apache Ant scripting for build processes. You need to cement your basic understanding of XML before delving into this book.

tip.eps If you need an introduction to XML, check out XML For Dummies, by Lucinda Dykes and Ed Tittel (John Wiley & Sons, Inc.).

If you already know how to use Java and XML, congratulations — you’re ahead of the curve.

Java: Your Android programming language

Android applications are written in Java — not the full-blown version of Java that’s familiar to developers using Java Platform, Enterprise Edition (J2EE), but a subset of the Java libraries that are specific to Android. This smaller subset of Java excludes classes that aren’t suitable for mobile devices. If you have experience in Java, you should feel right at home developing apps in Android.

Even with a Java reference book on hand, you can always search at www.google.com to find information about topics you don’t understand. Because Java isn’t a new language, you can find plenty of examples on the web that demonstrate how to do virtually anything.

remember.eps Not every class that’s available to Java programmers is available also on Android. Verify that it’s available to you before you start trying to use it. If it’s not, an alternative is probably bundled with Android that can work for your needs.

Activities

An Android application can consist of only a single activity or several. An activity serves as a container for both the user interface and the code that runs it. You can think of activities as pages of your app — one page in your app corresponds to one activity. Activities are discussed in more detail in Chapters 3 and 5.

Intents

Intents make up the core message system that runs Android. An intent is composed of two elements:

check.png An action: The general action to be performed (such as view, edit, or dial) when the intent is received

check.png Data: The information that the action operates on, such as the name of a contact

Intents are used to start activities and to communicate among various parts of the Android operating system. An application can either broadcast an intent or receive an intent.

Sending messages with intents

When you broadcast an intent, you send a message telling Android to make something happen. The intent can tell Android to start a new activity from within your application or to start another application.

Registering intent receivers

Sending an intent doesn’t make something happen automatically. You have to register an intent receiver that listens for the intent and then tells Android what to do — whether the task is starting a new activity or another app. If more than one receiver can accept a given intent, a chooser can be created to allow the user to choose which app to use to complete the activity — such as how the YouTube app allows the user to choose whether to watch videos in the YouTube app or in a browser.

Various registered receivers, such as the Gmail and the Messaging apps, handle image-sharing intents by default. When you find more than one possible intent receiver, a chooser opens with a list of options to choose from and asks what to do: Use e-mail, messaging, or another application, as shown in Figure 1-1.

Figure 1-1: A chooser.

9781118417454-fg0101.eps

warning_bomb.eps Follow best practice and create choosers for intents that don’t target other activities within your application. If the Android system cannot find a match for an intent that was sent, and if a chooser wasn’t created manually, the application crashes after experiencing a run-time exception — an unhandled error in the application. (Android expects developers to know what they’re doing.)

Cursorless controls

Unlike the PC, where you manipulate the mouse to move the cursor, an Android device lets you use your fingers to do nearly anything you can do with a mouse. Rather than right-click in Android, however, you long-press an element until its context menu appears.

As a developer, you can create and manipulate context menus. You can allow users to use two fingers on an Android device, rather than a single mouse cursor, for example. Fingers come in all sizes, so design the user interface in your apps accordingly. Buttons should be large enough (and have sufficient spacing) so that even users with larger fingers can interact with your apps easily, whether they’re using your app on a phone or tablet.

Views

A view, which is a basic element of the Android user interface, is a rectangular area of the screen that’s responsible for drawing and event handling. Views are a basic building block of Android user interfaces, much like paragraph <p> or anchor <a> tags are building blocks of an HTML page. Some common views you might use in an Android application might be a TextView, ImageView, Layout, and Button, but there are dozens more out there for you to explore.

Many more views are ready for you to use. For complete details about views, check out the android.widget and android.view packages in the Android documentation at http://developer.android.com/reference/android/widget/package-summary.html .

Asynchronous calls

You use the AsyncTask class in Android to run multiple operations at the same time without having to manage a separate thread yourself. The AsyncTask class not only lets you start a new process without having to clean up after yourself but also returns the result to the activity that started it — creating a clean programming model for asynchronous processing. In general, we use loaders in this book rather than AsyncTasks, but it’s useful to know about AsyncTasks for those occasional cases where a loader won’t do what you want.

remember.eps A thread is a process that runs separately from, but simultaneously with, everything else that’s happening.

You use asynchronous processing for tasks that might take more than a small fraction of a second, such as network (Internet) communication; reading from, or writing to, storage; or media processing. When users have to wait for your task to complete, use an asynchronous call and an element in the user interface to notify them that something is happening.

warning_bomb.eps Failing to use an asynchronous programming model can cause users of your application to believe that it’s buggy. Downloading the latest Twitter messages via the Internet takes time, for example. If the network slows and you aren’t using an asynchronous model, the application will lock up and the user will likely assume that something is wrong because the application isn’t responding to her interaction. If the application fails to respond within a reasonable length of time (defined by the Android operating system), the user sees the Application Not Responding (ANR) dialog box, as shown in Figure 1-2. The user can then choose whether to close the application or wait for it to recover.

Figure 1-2: The ANR dialog box.

9781118417454-fg0102.tif

tip.eps To follow the best practice, run CPU-intensive or long-running code inside another thread, as described in the Designing for Responsiveness page on the Android developer site (http://developer.android.com/guide/practices/design/responsiveness.html ).

Background services

If you’re a Windows user, you may already know what a service is: an application that runs in the background and doesn’t necessarily have a user interface. A classic example is an antivirus application that usually runs in the background as a service. Even though you don’t see it, you know that it’s running.

Most music players that can be downloaded from the Google Play Store, for example, run as background services. Users can then listen to music while checking e-mail or performing other tasks that require the use of the screen.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset