Dave MacLean | Satya Komatineni | Grant Allen Use this definitive

Dave MacLean | Satya Komatineni | Grant Allen Use this definitive




ro Android 5 shows you how to build real-world and fun mobile apps using the Android 5.0 SDK. This book covers everything from the fundamentals of building apps for embedded devices, smartphones, and tablets to advanced concepts such as custom components, multi-tasking, sensors/augmented reality, better accessories support and much more.

Using the tutorials and expert advice, you’ll quickly be able to build cool mobile apps and run them on dozens of Android-based smartphones

You’ll explore and use the Android APIs, including those for media and sensors

And you’ll check out what’s new in Android, including the improved user interface across all Android platforms, integration with services, and more

By reading this definitive tutorial and reference, you’ll gain the knowledge and experience to create stunning, cutting-edge Android apps that can make you money, while keeping you agile enough to respond to changes in the future.

How to use Android to build Java-based mobile apps for Android smartphones and tablets

How to build irresistible user interfaces (UIs) and user experiences (UXs) across Android devices

How to populate your application with data from data sources, using

Content Providers

How to build multimedia and game apps using Android’s media APIs

How to use Android’s location-based services, network-based services, and security

How to use key Android features, such as Fragments and the ActionBar



US $49.99

Shelve in

Mobile Computing

User level:


ISBN 978-1-4302-4680-0

5 4 9 9 9

9 781430 246800

Use this definitive Android reference to take your apps further


Android 5

Dave MacLean


Satya Komatineni


Grant Allen

Foreword by Grant Allen, Google


For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them.


Contents at a Glance

About the Authors ������������������������������������������������������������������������������������������������ xxvii

About the Technical Reviewer ������������������������������������������������������������������������������


Acknowledgments �����������������������������������������������������������������������������������������������


Foreword ������������������������������������������������������������������������������������������������������������


Introduction ��������������������������������������������������������������������������������������������������������� xxxv

Chapter 1


Hello Android ���������������������������������������������������������������������������������������


Chapter 2


Introduction to Android Application Architecture �����������������������������

� 29

Chapter 3


Building Basic User Interfaces and Using Controls ���������������������������


Chapter 4


Adapters and List Controls ���������������������������������������������������������������� 99

Chapter 5


Building More Advanced UI Layouts ������������������������������������������������


Chapter 6


Working with Menus and Action Bars ���������������������������������������������


Chapter 7


Styles and Themes ���������������������������������������������������������������������������


Chapter 8


Fragments ����������������������������������������������������������������������������������������


Chapter 9


Responding to Configuration Changes ���������������������������������������������


Chapter 10


Working with Dialogs ��������������������������������������������������������������������


Chapter 11


Working with Preferences and Saving State ���������������������������������





Contents at a Glance

Chapter 12


Using the Compatibility Library for Older Devices �������������������������


Chapter 13


Exploring Packages, Processes, Threads, and Handlers ��������������� 247

Chapter 14


Building and Consuming Services �������������������������������������������������


Chapter 15


Advanced AsyncTask and Progress Dialogs ����������������������������������


Chapter 16


Broadcast Receivers and Long-Running Services �������������������������


Chapter 17


Exploring the Alarm Manager ��������������������������������������������������������


Chapter 18


Exploring 2D Animation �����������������������������������������������������������������


Chapter 19


Exploring Maps and Location-Based Services ������������������������������


Chapter 20


Understanding the Media Frameworks������������������������������������������


Chapter 21


Home Screen Widgets ��������������������������������������������������������������������


Chapter 22


Touch Screens ��������������������������������������������������������������������������������


Chapter 23


Implementing Drag and Drop ���������������������������������������������������������


Chapter 24


Using Sensors ��������������������������������������������������������������������������������


Chapter 25


Exploring Android Persistence and Content Providers �����������������

� 559

Chapter 26


Understanding Loaders ������������������������������������������������������������������


Chapter 27


Exploring the Contacts API ������������������������������������������������������������ 621

Chapter 28


Exploring Security and Permissions ����������������������������������������������


Chapter 29


Using Google Cloud Messaging with Android ��������������������������������


Chapter 30


Deploying Your Application: Google Play Store and Beyond ����������


Index ���������������������������������������������������������������������������������������������������������������������




Welcome to the wonderful world of Android. A world where, with a bit of knowledge and effort, you too can write Android applications. To write good applications, however, you will need to dig deeper, to understand the fundamentals of the Android architecture, to understand how applications can work together, to understand how mobile applications are different from all previous forms of programming. The online documentation on Android is fair, but it does not go far enough. You can read the source code, but that's not at all easy.

This book is the culmination of seven years of researching, developing, testing, refining, and writing about Android. We’ve read all the online documentation, scoured through source code, explored the far reaches of the Internet, and have compiled this book. We’ve filled in the gaps, anticipated the questions you have, and provided answers. Along the way we’ve seen APIs come and go and be revised. We’ve seen major changes in how applications are constructed. At first we all used Activities, but when tablets came along we started using Fragments. We've taken everything we’ve learned and filled this book with practical guidance to using the latest Android APIs to write interesting applications.

You will still find coverage of the beginning topics, to help the new learner get started developing for Android. You will also find coverage of the more advanced topics, such as

Google Maps Android API v2, which is very different from v1. We’ve updated this edition with the latest information on the available APIs. You will find in-depth coverage of intents, services, broadcast receivers, communication, fragments, widgets, sensors, animation, security, loaders, persistence, Google Cloud Messaging, audio and video, and more. And for every topic there are sample programs that illustrate each API in meaningful ways. All source code is downloadable, so you can copy and paste it into your applications to get a great head start.





Hello Android

Welcome to the book, and welcome to the world of Android development. In a little under ten years, Android has helped change the face of modern mobile computing and telephony and launched a revolution in how applications are developed, and by whom. With this book in your hands, you are now part of the great Android explosion! We’re going to assume that you want to get straight at working with Android, so we're not going to bore you with a fireside chat about Android's history, major characters, plaudits, or any other prose. We're going to get straight to it!

In this chapter, you’ll start by seeing what you need to begin building applications with the Android software development kit (SDK) and set up your choice of development environment. Next, you step through a “Hello World!” application. Then the chapter explains the Android application life cycle and ends with a discussion about running your applications with Android Virtual Devices (AVDs) and on real devices. So let’s get started.

Prerequisites for Android Development

To build applications for Android, you need the Java SE Development Kit (JDK), the Android

SDK, and a development environment. Strictly speaking, you can develop your applications using nothing more than a primitive text editor and a handful of command-line tools like

Ant. For the purposes of this book, we’ll use the commonly available Eclipse IDE, though you are free to adopt Android Studio and its IntelliJ underpinnings—we’ll even walk through

Android Studio for those who have not seen it. With the exception of a few add-on tools, the examples we share in the book will work equally well between these two IDEs.

The Android SDK requires JDK 6 or 7 (the full JDK, not just the Java Runtime Environment

[JRE]) and optionally a supported IDE. Currently, Google directly supports two alternative

IDEs, providing some choice. Historically, Eclipse was the first IDE supported by Google for Android development, and developing for Android 4.4 KitKat or 5.0 Lollipop requires

Eclipse 3.6.2 or higher (this book uses Eclipse 4.2 or 4.4, also known as Juno and Luna, respectively, and other versions). The alternative environment released and supported by

Google for Android is now known as Android Studio. This is a packaged version of IDEA

IntelliJ with built-in Android SDK and developer tools.



2 CHAPTER 1: Hello Android

Note At the time of this writing, Java 8 was available but not yet supported by the Android SDK.

In previous versions of the Android SDK, Java 5 was also supported, but this is no longer the case.

The latest version of Eclipse (4.4, a.k.a. Juno) was also available, but Android has historically not been reliable on the latest Eclipse right away. Check the system requirements here to find the latest: http://developer.android.com/sdk/index.html


The Android SDK is compatible with Windows (Windows XP, Windows Vista, and Windows 7),

Mac OS X (Intel only), and Linux (Intel only). In terms of hardware, you need an Intel machine, the more powerful the better.

To make your life easier, if you choose Eclipse as your IDE, you will want to use Android development tools (ADT). ADT is an Eclipse plug-in that supports building Android applications with the Eclipse IDE.

The Android SDK is made up of two main parts: the tools and the packages. When you first install the SDK, all you get are the base tools. These are executables and supporting files to help you develop applications. The packages are the files specific to a particular version of Android (called a platform) or a particular add-on to a platform. The platforms include

Android 1.5 through 4.4.2. The add-ons include the Google Maps API, the Market License

Validator, and even vendor-supplied ones such as Samsung’s Galaxy Tab add-on. After you install the SDK, you then use one of the tools to download and set up the platforms and add-ons.

Remember, you only need to set up and configure one of Eclipse or Android Studio. You can use both if you are so inclined, but it’s certainly not required. Let’s get started!

Setting Up Your Eclipse Environment

In this section, you walk through downloading JDK 6, the Eclipse IDE, the Android SDK

(tools and packages), and ADT. You also configure Eclipse to build Android applications.

Google provides a page to describe the installation process ( http://developer.android.


) but leaves out some crucial steps, as you will see.

Downloading JDK

The first thing you need is the JDK. The Android SDK requires JDK 6 or higher; we’ve developed our examples using JDK 6 and 7, depending on the version of Eclipse or Android

Studio in use. For Windows and Mac OS X, download JDK 7 from the Oracle web site

( www.oracle.com/technetwork/java/javase/

downloads/index.html) and install it. You only need the JDK, not the bundles. To install the JDK for Linux, open a Terminal window and instruct your package manager to install it. For example, in Debian or Ubuntu try the following: sudo apt-get install sun-java7-jdk www.it-ebooks.info

CHAPTER 1: Hello Android 3

This should install the JDK plus any dependencies such as the JRE. If it doesn’t, it probably means you need to add a new software source and then try that command again. The web page https://help.ubuntu.com/community/Repositories/Ubuntu

explains software sources and how to add the connection to third-party software. The process is different depending on which version of Linux you have. After you’ve done that, retry the command.

With the introduction of Ubuntu 10.04 (Lucid Lynx), Ubuntu recommends using OpenJDK instead of the Oracle/Sun JDK. To install OpenJDK, try the following: sudo apt-get install openjdk-7-jdk

If this is not found, set up the third-party software as outlined previously and run the command again. All packages on which the JDK depends are automatically added for you.

It is possible to have both OpenJDK and the Oracle/Sun JDK installed at the same time. To switch active Java between the installed versions of Java on Ubuntu, run this command at a shell prompt sudo update-alternatives --config java and then choose which Java you want as the default.

Now that you have a Java JDK installed, it’s time to set the JAVA_HOME environment variable to point to the JDK install folder. To do this on a Windows XP machine, choose Start ➤ My

Computer, right-click, select Properties, choose the Advanced tab, and click Environment

Variables. Click New to add the variable or Edit to modify it if it already exists. The value of

JAVA_HOME is something like C:\Program Files\Java\jdk1.7.0_79.

For Windows Vista and Windows 7, the steps to get to the Environment Variables screen are a little different. Choose Start ➤ Computer, right-click, choose Properties, click the link for Advanced System Settings, and click Environment Variables. After that, follow the same instructions as for Windows XP to change the JAVA_HOME environment variable.

For Mac OS X, you set JAVA_HOME in the .bashrc file in your home directory. Edit or create the

.bashrc file, and add a line that looks like this export JAVA_HOME=path_to_JDK_directory where path_to_JDK_directory is probably /Library/Java/Home. For Linux, edit your .bashrc file and add a line like the one for Mac OS X, except that your path to Java is probably something like /usr/lib/jvm/java-6-sun or /usr/lib/jvm/java-6-openjdk.

Downloading Eclipse

After the JDK is installed, you can download the Eclipse IDE for Java Developers.

(You don’t need the edition for Java EE; it works, but it’s much larger and includes things you don’t need for this book.) The examples in this book use Eclipse 4.2 or 4.4 (on both

Linux and Windows environments). You can download all versions of Eclipse from www.eclipse.org/downloads/



4 CHAPTER 1: Hello Android

Note As an alternative to the individual steps presented here, you can also download the ADT

Bundle from the Android developer site. This includes Eclipse with built-in developer tools and the

Android SDK in one package. It’s a great way to get started quickly, but if you have an existing environment, or just want to know how all the components are stitched together, then following the step-by-step instructions is the way to go.

The Eclipse distribution is a .zip file that can be extracted just about anywhere. The simplest place to extract to on Windows is C:\, which results in a C:\eclipse folder where you find eclipse.exe. Depending on your security configuration, Windows may insist on enforcing UAC when running from C:\. For Mac OS X, you can extract to Applications. For

Linux, you can extract to your home directory or have your administrator put Eclipse into a common place where you can get to it. The Eclipse executable is in the eclipse folder for all platforms. You may also find and install Eclipse using Linux’s Software Center for adding new applications, although this may not provide you with the latest version.

When you first start up Eclipse, it asks you for a location for the workspace. To make things easy, you can choose a simple location such as C:\android or a directory under your home directory. If you share the computer with others, you should put your workspace folder somewhere underneath your home directory.

Downloading the Android SDK

To build applications for Android, you need the Android SDK. As stated before, the SDK comes with the base tools; then you download the package parts that you need and/or want to use. The tools part of the SDK includes an emulator so you don’t need a mobile device with the Android OS to develop Android applications. It also has a setup utility to allow you to install the packages that you want to download.

You can download the Android SDK from http://developer.android.com/sdk

. It ships as a

.zip file, similar to the way Eclipse is distributed, so you need to unzip it to an appropriate location. For Windows, unzip the file to a convenient location (we used the C: drive), after which you should have a folder called something like C:\android-sdk-windows that contains the files as shown in Figure

1-1 . For Mac OS X and Linux, you can unzip the file to your home

directory. Notice that Mac OS X and Linux do not have an SDK Manager executable; the equivalent of the SDK Manager in Mac OS X and Linux is to run the tools/android program.

Figure 1-1. Base contents of the Android SDK


CHAPTER 1: Hello Android 5

An alternative approach (for Windows only) is to download an installer EXE instead of the zip file and then run the installer executable. This executable checks for the Java JDK, unpacks the embedded files for you, and runs the SDK Manager program to help you set up the rest of the downloads.

Whether through using the Windows installer or by executing the SDK Manager, you should install some packages next. When you first install the Android SDK, it does not come with any platform versions (that is, versions of Android). Installing platforms is pretty easy. After you’ve launched the SDK Manager, you see what is installed and what’s available to install, as shown in Figure


. You must add Android SDK tools and platform-tools in order for your environment to work. Because you use it shortly, add at least the Android 1.6 SDK platform, as well as the latest platform shown in your installer.

Figure 1-2. Adding packages to the Android SDK

Click the Install button. You need to click Accept for each item you’re installing (or Accept

All) and then click Install. Android then downloads your packages and platforms to make them available to you. The Google APIs are add-ons for developing applications using

Google Maps. You can always come back to add more packages later.


6 CHAPTER 1: Hello Android

Updating Your PATH Environment Variable

The Android SDK comes with a tools directory that you want to have in your PATH. You also need in your PATH the platform-tools directory you just installed. Let’s add them now or, if you’re upgrading, make sure they’re correct. While you’re there, you can also add a JDK bin directory, which will make life easier later.

For Windows, get back to the Environment Variables window. Edit the PATH variable and add a semicolon (;) on the end, followed by the path to the Android SDK tools folder, followed by another semicolon, followed by the path to the Android SDK platform-tools folder, followed by another semicolon, and then %JAVA_HOME%\bin. Click OK when you’re done. For Mac OS X and Linux, edit your .bashrc file and add the Android SDK tools directory path to your PATH variable, as well as the Android SDK platform-tools directory and the $JAVA_HOME/bin directory. Something like the following works for Linux: export PATH=$PATH:$HOME/android-sdk-linux_x86/tools:$HOME/android-sdk-linux_x86/platformtools:$JAVA_HOME/bin

Just make sure that the PATH component that’s pointing to the Android SDK tools directories is correct for your particular setup.

The Tools Window

Later in this book, there are times when you need to execute a command-line utility program. These programs are part of the JDK or part of the Android SDK. By having these directories in your PATH, you don’t need to specify the full pathnames in order to execute them, but you need to start up a tools window in order to run them (later chapters refer to this tools window). The easiest way to create a tools window in Windows is to choose Start

Run, type in cmd, and click OK. For Mac OS X, choose Terminal from your Applications folder in Finder or from the Dock if it’s there. For Linux, run your favorite terminal.

You may need to know the IP address of your workstation later. To find this in Windows, launch a tools window and enter the command ipconfig. The results contain an entry for IPv4 (or something like that) with your IP address listed next to it. An IP address looks something like this: For Mac OS X and Linux, launch a tools window and use the command ifconfig. You find your IP address next to the label inet addr.

You may see a network connection called localhost or lo; the IP address for this network connection is This is a special network connection used by the operating system and is not the same as your workstation’s IP address. Look for a different number for your workstation’s IP address.


CHAPTER 1: Hello Android 7

Installing ADT

Now you need to install ADT (very recently renamed to GDT, the Google Developer Tools), an

Eclipse plug-in that helps you build Android applications. Specifically, ADT integrates with

Eclipse to provide facilities for you to create, test, and debug Android applications. You need to use the Install New Software facility in Eclipse to perform the installation. (The instructions for upgrading ADT appear later in this section.) To get started, launch the Eclipse IDE and follow these steps:

1. Select Help ➤ Install New Software.

2. Select the Work With field, type in https://dl-ssl.google.com/android/eclipse/ , and press Enter. Eclipse contacts the site and populates the list as shown in Figure



Figure 1-3. Installing ADT using the Install New Software feature in Eclipse


8 CHAPTER 1: Hello Android

3. You should see an entry named Developer Tools with four child nodes: Android DDMS, Android Development Tools, Android

Hierarchy Viewer, and Android Traceview. Just before publishing this book, Google updated the ADT to be part of the more generic

Google Developer Tools plugin for Eclipse, or GDT. Look for the same options in the GDT. Select the parent node Developer Tools, make sure the child nodes are also selected, and click the Next button.

The versions you see may be newer than these, and that’s okay.

You may also see additional tools. These tools are explained further in Chapter 11.

4. Eclipse asks you to verify the tools to install. Click Next.

5. You’re asked to review the licenses for ADT as well as for the tools required to install ADT. Review the licenses, click “I accept,” and then click the Finish button.

Eclipse downloads the developer tools and installs them. You need to restart Eclipse for the new plug-in to show up in the IDE.

If you already have an older version of ADT in Eclipse, go to the Eclipse Help menu and choose Check for Updates. You should see the new version of ADT and be able to follow the installation instructions, picking up at step 3.

Note If you’re doing an upgrade of ADT, you may not see some of these tools in the list of tools

to be upgraded. If you don’t see them, then after you’ve upgraded the rest of the ADT, go to Install

New Software and select https://dl-ssl.google.com/android/eclipse/

from the Works

With menu. The middle window should show you other tools that are available to be installed.

The final step to make ADT functional in Eclipse is to point it to the Android SDK. In Eclipse, select Window ➤ Preferences. (On Mac OS X, Preferences is under the Eclipse menu.) In the

Preferences dialog box, select the Android node and set the SDK Location field to the path

of the Android SDK (see Figure 1-4 ) and then click the Apply button. Note that you may see

a dialog box asking if you want to send usage statistics to Google concerning the Android

SDK; that decision is up to you.


CHAPTER 1: Hello Android 9

Figure 1-4. Pointing ADT to the Android SDK

You may want to make one more Preferences change on the Android ➤ Build page. The

Skip Packaging option should be checked if you’d like to make your file saves faster. By default, the ADT readies your application for launch every time it builds it. By checking this option, packaging and indexing occur only when truly needed.

From Eclipse, you can launch the SDK Manager. To do so, choose Window ➤ Android SDK

Manager. You should see the same window as in Figure



If you’ve chosen Eclipse as your IDE, you are almost ready for your first Android application—you can skip the following section on Android Studio and head straight to the

“Learning Android’s Fundamental Components” section.

Setting Up Your Android Studio Environment

In 2013, Google introduced a second supported development environment, known as

Android Studio (or Android Developer Studio at the time of launch). This is based around a popular Java IDE: IDEA IntelliJ. The most important thing to know about Android Studio is that it is still a work in progress. As of this book’s writing, the latest version is 1.2. Anyone familiar with the vagaries of version numbers knows that starting with a low number usually means “beware!” www.it-ebooks.info

10 CHAPTER 1: Hello Android

The second most important thing to remember is that Android Studio currently assumes a

64-bit development environment. That means dependencies like Java also need to be 64-bit.

The next sections briefly cover the setup of Android Studio for those interested or gung-ho enough to use it. Be mindful that the rest of the book predominantly shows examples and options using Eclipse.

Java requirements for Android Studio

Like Eclipse, Android Studio relies on a working Java installation. Android Studio will attempt to automatically discover your Java environment during installation, so it pays to have Java installed and configured.

For Java installation, remember that Android Studio is 64-bit. In all other respects, you can follow the preceding section titled “Downloading JDK”—we won’t repeat that word-for-word here to save some trees. Ensure you follow all the instructions there, including setting the

JAVA_HOME environment variable, as this is the main indicator used by the Android Studio installer to find your Java installation.

Downloading and Installing Android Studio

Google makes Android Studio available from the main Android development site, currently at the URL http://developer.android.com/sdk/installing/studio.html

. That may change at any time, but a quick search on the developer.android.com site should find it. Android Studio is packaged as a monolithic bundle, with nearly all the components you need. The Java SDK is the exception—we’ll cover that shortly. The package downloaded from the preceding URL will be named something like android-studio-bundle-132.893413-windows.exe for windows, or a similar name with a different extension for OS X and Linux, and includes the following:

 Current latest build of the Android Studio bundle of IntelliJ IDEA

 Built-in Android SDK

 All related Android build tools

 Android Virtual Device images

We’ll talk more about these components in later chapters. For a Windows installation run the executable and follow the prompts to choose an installation path, and decide whether

Android Studio is made available to all users on the Windows machine, or just the current user. For OS X, open the .dmg file and copy the Android Studio entry to your Applications folder. Under Linux, extract the contents of the .tgz file to your desired location.

Once installed, you can start Android Studio under Windows from the start menu folder you chose when prompted; under OS X from the Applications folder; and under Linux by running the ./android-studio/bin/studio.sh file under your installation directory. Whatever the

operating system, you should see the Android Studio home screen as depicted in Figure 1-5 .


CHAPTER 1: Hello Android 11

Figure 1-5. Android Studio when first launched

Learning Android’s Fundamental Components

Every application framework has some key components that developers need to understand before they can begin to write applications based on the framework. For example, you need to understand JavaServer Pages (JSP) and servlets in order to write Java 2 Platform,

Enterprise Edition (J2EE) applications. Similarly, you need to understand views, activities, fragments, intents, content providers, services, and the AndroidManifest.xml file when you build applications for Android. You briefly cover these fundamental concepts here and explore them in more detail throughout the book.


12 CHAPTER 1: Hello Android


Views are user interface (UI) elements that form the basic building blocks of a user interface.

A view can be a button, a label, a text field, or many other UI elements. If you’re familiar with views in J2EE and Swing, then you understand views in Android. Views are also used as containers for views, which means there’s usually a hierarchy of views in the UI. In the end, everything you see is a view.


An activity is a UI concept that usually represents a single screen in your application. It generally contains one or more views, but it doesn’t have to. An activity is pretty much like it sounds—something that helps the user do one thing, which could be viewing data, creating data, or editing data. Most Android applications have several activities within them.


When a screen is large, it becomes difficult to manage all of its functionality in a single activity. Fragments are like sub-activities, and an activity can display one or more fragments on the screen at the same time. When a screen is small, an activity is more likely to contain just one fragment, and that fragment can be the same one used within larger screens.


An intent generically defines an “intention” to do some work. Intents encapsulate several concepts, so the best approach to understanding them is to see examples of their use. You can use intents to perform the following tasks:

Broadcast a message

Start a service

Launch an activity

Display a web page or a list of contacts

Dial a phone number or answer a phone call

Intents are not always initiated by your application—they’re also used by the system to notify your application of specific events (such as the arrival of a text message).

Intents can be explicit or implicit. If you simply say that you want to display a URL, the system decides what component will fulfill the intention. You can also provide specific information about what should handle the intention. Intents loosely couple the action and action handler.


CHAPTER 1: Hello Android 13

Content Provider

Data sharing among mobile applications on a device is common. Therefore, Android defines a standard mechanism for applications to share data (such as a list of contacts) without exposing the underlying storage, structure, and implementation. Through content providers, you can expose your data and have your applications use data from other applications.


Services in Android resemble services you see in Windows or other platforms—they’re background processes that can potentially run for a long time. Android defines two types of services: local services and remote services. Local services are components that are only accessible by the application that is hosting the service. Conversely, remote services are services that are meant to be accessed remotely by other applications running on the device.

An example of a service is a component that is used by an e-mail application to poll for new messages. This kind of service may be a local service if the service is not used by other applications running on the device. If several applications use the service, then it’s implemented as a remote service.


AndroidManifest.xml, which is similar to the web.xml file in the J2EE world, defines the contents and behavior of your application. For example, it lists your application’s activities and services, along with the permissions and features the application needs to run.


An AVD allows developers to test their applications without hooking up an actual Android device (typically a phone or a tablet). AVDs can be created in various configurations to emulate different types of real devices.

Hello World!

Now you’re ready to build your first Android application. You start by building a simple “Hello

World!” program. Create the skeleton of the application by following these steps:

1. Launch Eclipse, and select File ➤ New ➤ Project. In the New Project dialog box, select Android Application Project and then click Next. You see the New Android Project dialog box, as shown in Figure

1-6 . (Eclipse

may have added Android Project to the New menu, so you can use it if it’s there.) There’s also a New Android Project button on the toolbar.


14 CHAPTER 1: Hello Android

Figure 1-6. Using the New Project Wizard to create an Android application

2. As shown in Figure


, enter

HelloAndroid as the project name.

You need to distinguish this project from other projects you create in

Eclipse, so choose a name that will make sense to you when you are looking at all the projects in your Eclipse environment. You will also see the available Build Targets. Select Android 2.2. This is the version of Android you use as your base for the application. You can run your application on later versions of Android, such as 4.3 and 4.4; but Android 2.2 has all the functionality you need for this example, so choose it as your target. In general, it’s best to choose the lowest version number you can, because that maximizes the number of devices that can run your application.

3. Leave the Project Name to auto-complete itself based on your

Application Name.

4. Use

com.androidbook.hello as the package name. Like all Java

applications, your application must have a base package name, and this is it. This package name will be used as an identifier for your application and must be unique across all applications. For this reason, it’s best to start the package name with a domain name that you own. If you don’t own one, be creative to ensure that your package name won’t likely be used by anyone else. Click Next.


Was this manual useful for you? yes no
Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Download PDF