Figure 1-1: A chooser.
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.
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.
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.
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:
An action: The general action to be performed (such as view, edit, or dial) when the intent is received
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.
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.
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.
Figure 1-2: The ANR dialog box.
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.