Google
Learn to create applications for mobile devices
with the Android SDK
2nd Edition
Ricardo R. Lecheta
Novatec
chapter 1
Introduction to Android
1.1 Introduction
The cell phone market is growing more and more. Studies show that today in
more than 3 billion people have a mobile phone, and this corresponds
about half of the world's population.
Nowadays, common users are increasingly looking for cell phones with
various resources such as cameras, music, bluetooth, great visual interface, games,
GPS, internet access and emails, and now we also have digital TV.
The corporate market is also growing very much, and various companies are
looking to incorporate mobile applications into your daily routine to streamline your business and
integrate mobile applications with their back-end systems. Companies obviously
visam profit and more profit, cell phones and smartphones can occupy an important
space in a world where the word "mobility" is becoming more and more known.
In this way, applications that run on a mobile phone can be literally
connected and online, synchronizing information directly from a server con-
reliable company. Nowadays, several banks offer services to their users.
banks, where you can pay your bills and view your checking account statement
directly from a phone. More developed countries already allow phones
to be used in markets to read the barcodes of products and carry out
the purchase just with the cell phone, as if it were a credit card. What everyone
what they actually needed was a powerful and flexible platform to make all of this happen
more viable and increasingly a reality for everyone.
Companies and developers are looking for a modern and agile platform for
development of corporate applications to assist in your business and profits.
19
20 Google Android
Common users look for a cellphone with a sleek and modern design,
easy navigation and a plethora of features. Now, is there anyone to
to address these two completely different worlds?
To keep up with this evolution of technology and satisfy users, the manufacturers
singers and cell phone operators, the companies and us, the developers, there is a
great race starred by the biggest companies in the world in mobile technology
to compete for this market niche.
Android is Google's response to occupy this space. It consists of a
new development platform for mobile applications, based on a system
Linux operational, with various applications already installed and, still, an environment of
quite powerful, bold, and flexible development.
Android made a big impact when it was announced, attracting attention
from many people. We can say this happened because behind it is nothing
nothing less than Google, the company that is revolutionizing the internet.
Meanwhile, it is not just Google that is in the game, but rather a group formed
by leading telecommunications companies such as Motorola, LG, Samsung,
Sony Ericsson and many others. This group, called the Open Handset Alliance
(OHA) was created with the intention of standardizing an open source platform.
and free for mobile phones, precisely to meet all expectations and trends
of the current market.
There are several interesting videos on YouTube that demonstrate some
Android resources, and some are impressive. The videos show some of them.
more basic features such as internet browsing with the touch screen,
barcode recognition, a demonstration of the famous game Quake
running perfectly with great resolution and much more.
One of these videos features one of the classic resources of the platform and, in of-
finished moment, the host says something like: 'Hmm, I'm hungry, I like
eating sushi, is there a Japanese restaurant around here?” And he accesses
Google Maps to search for the nearest restaurants, using
GPS makes it possible to know your exact location and plan the route to the nearest restaurant.
next desired. That's it: cell phones are no longer just used to answer
connections, now being true machines filled with technology for you to enjoy.
This book focuses on the development of applications for Android, using the
Java language and version 2.2 of the SDK. In each chapter, new concepts and ideas
will be presented, so that you can get ahead and be prepared for when
this market will explode.
Chapter■1 Introduction to Android 21
1.2 Open Handset Alliance and Android
The Open Handset Alliance (OHA) is a group made up of market giants.
of mobile telecommunications led by Google. Among some members of the group
there are established names like HTC, LG, Motorola, Samsung, Sony Ericsson,
Toshiba, Sprint Nextel, China Mobile, T-Mobile, ASUS, Intel, Garmin and much more.
When this book was written, the group was made up of 47 key members, and
you can check the complete list at this address:
[Link]
On the OHA site, there is a great description of what this alliance would be. The text
Today, there are 1.5 billion
a billion television sets in use around the world and 1 billion people have
access to the internet. However, nearly 3 billion people have a mobile phone,
making the device one of the most successful consumer products in the world.
In this way, building a superior cell phone would improve the lives of countless people.
people around the world. The Open Handset Alliance is a group formed by
leading companies in mobile technology that share this vision to change the
mobile experience of all consumers [...]
Thus, the group's objective is to define a unique and open platform for ce-
hints to make consumers more satisfied with the final product. Another
the main objective of this alliance is to create a modern and flexible platform for the
development of corporate applications. The result of this union, like you
You probably already know, it was the birth of Android.
Android is the new development platform for mobile applications.
like smartphones and contain an operating system based on Linux, a
rich visual interface, GPS, various applications already installed and still an environment
of quite powerful, innovative, and flexible development. Another good piece of news is
what we can use the renowned Java language for developing applications,
enjoying all the resources we are entitled to.
Everyone ends up benefiting from this union: the cell phone manufacturers, the users
common areas and, of course, application developers.
Mobile phone users are extremely favored by all of this. Today
Nowadays, everyone wants a mobile phone with a good appearance, easy usability, with
touch screen, camera, music, games, GPS, internet access and much more, and the
cell phone is increasingly occupying an important space in people's lives. The
Android was created with the intention of pleasing these users, enabling
that find all the expected resources in just one device. The world of
22 Google Android
technology is always evolving, and the OHAwas created precisely to maintain a
standard platform where all new market trends are encompassed
in a single solution.
For mobile phone manufacturers, the existence of a single and consolidated platform
Lidada is a great advantage for creating new devices. The great advantage for
they say that the platform is also free and open source. The Android license is
flexible and allows each manufacturer to make changes to the source code to
customize your products, and the best part is, without the need to share
these changes with no one. Android is also "free," and manufacturers can
to use and abuse it without having to pay for it.
The fact that Android is open source greatly contributes to its improvement.
mento, since developers from all over the world can contri-
to your source code, adding new features or simply
correcting failures.
Application developers can enjoy a platform for
modern development with various incredible resources, with everything that exists
more modern. This is the theme of this book: the development of applications with the
Android. And here you will understand the reason for this entire revolution.
1.3 Getting to know a little more about Android
Every cell phone has a home screen with some icons and a menu, right? Every cell phone
It also has a contacts agenda and a screen to make the call, right?
Now, have you thought about replacing some of these screens with a customized screen?
developed by you? With Android this is possible. Its architecture is very
flexible and you can integrate native applications with your application, or even
replace any existing native application with one that you created yourself. I believe
that mobile operators or manufacturers will like this feature, already
that in this way it is possible to change the screens and functionalities of the phone to make
that basic marketing.
It is possible to integrate applications in a simple way, whether they are developed
for you, let them be native applications. For example, imagine that your application needs
check the contact list to select a specific friend, and right after that
view his address on a map. Well, there is the contact list and
Everyone already knows about Google Maps on Android, but is it possible to use them and
to integrate them into our applications? The answer is yes. Integration is one of the key words-
keys in corporate applications, and theAndroid architecture was created precisely
thinking about it.
Chapter■1 Introduction to Android 23
Android has many interesting features and a truly flexible architecture.
focused on the integration of applications. There is no difference between a native application
it's developed by you.
Speaking of integration, there is a class that is the heart of Android, called
ofIntentand it will be explained in chapter 5. This class is nothing more than a
message sent to the operating system informing our "intention" to carry out
determined task.
So, in the Android operating system, messages are sent out to everyone.
the sides, identified by the classIntent. According to the content of the message, it
it can be intercepted by any interested application in order to carry out the task
what is necessary. For example, if you want to open a native application like the
browser or open a new screen of your application, the only thing you need to do
to create this objectIntentand configure the content of your message correctly
to be interpreted by the operating system.
Another strong point of Android is that its operating system is based on Linux.
and it itself takes care of managing memory and processes. This allows for
various applications can be run at the same time, allowing for apli-
background tasks can run without the user noticing, while
he is accessing the internet or taking a call. This feature is really cool
and you can easily schedule an application to run at a certain time
date and time, without interfering with the user's activity.
Of course, we cannot forget the visual resources and those functionalities.
that impress the average user. In this regard, Android certainly meets the
Expectations. Navigation through screens via the touch screen allows the user
have a great experience using the [Link] to reviews about the HTC G1,
the first Android phone launched on the market, internet browsing is amazing.
Android also supports 3D graphics based on specification 1.0.
OpenGL ES allows for creating games with excellent quality.
resolution. You can check out several game demonstrations on YouTube videos.
1.4 Linux operating system
The Android operating system is based on the Linux kernel 2.6, and is responsible
responsible for managing memory, processes, threads, and file security
folders, in addition to networks and drivers.
Each application on Android triggers a new process in the operating system.
Some of them may display a screen for the user, and others may run in the background.
24 Google Android
in the background for an indefinite period. Various processes and applications can
to be executed simultaneously, and the operating system kernel is responsible
for carrying out the entire memory control.
If necessary, the operating system itself may decide to terminate some
process to free memory and resources, and perhaps even restart the same process
later when the situation is under control.
All the security of Android is based on the security of Linux. In Android each
application runs in a single process and each process in turn has
a dedicated thread. For each application installed on the phone, a user is created
in the operating system to access its directory structure. In this way
no other user can access this application.
1.5 Dalvik Virtual Machine
You already know that the Java language is used to build applications for the
Android. The fact is that there is no virtual machine in its operating system.
Java (JVM). In fact, what we have is a virtual machine called Dalvik that
is optimized for execution on mobile devices.
When developing applications for Android, you will use the Java language.
and all of its resources normally, but after the bytecode (.class) is with-
he is converted to the [Link](Dalvik Executable), which represents the
compiled Android application.
After that, the .dex files and other resources like images are compressed
in a single file with the [Link](Android Package File), which represents
the final application, ready to be distributed and installed.
When using the Eclipse development environment, all this compilation and
file [Link] happens automatically, therefore, there is no need to worry
suffer from this.
1.6 Open and free code
Android is the first completely free platform for mobile applications and
open-source, which represents a great advantage for its
evolution, since various programmers from around the world will be able to contribute to
improve the platform.
Chapter■1 Introduction to Android 25
For mobile phone manufacturers, this is also a great advantage, since
that it is possible to use the Android operating system on your phones without having
What to pay for this. In addition, the Apache Software Foundation (ASF) license allows
what changes should be made to the source code to create customized products
without needing to share the changes with anyone.
You can get more information and even download the source code from
Android on the following site:No text to translate from the provided URL.
1.7 Android Developer Challenge
To promote Android, Google started investing heavily and, as soon as the
the first version of the SDK was released, the famous contest was also announced
Android Developer Challenge (ADC), with over $10 million in prizes.
The deadline for submitting applications was April 14, 2008, and the competition was divided
in two phases. In the first phase, the top 50 applications received $25,000
On Monday, 20 more of the best applications would be selected to receive
US$ 275,000, and some US$ 100,000.
In the first stage, the applications were tested on the Android emulator itself.
because at the time no cell phone with Android had been released. This was a
great move by Google to improve the platform and help test it, as
developers from around the world were interested in developing the applications
to perhaps win the jackpot. This contest literally shook the whole world, and
this allows Google to test the SDK and consolidate its product.
The second part was announced to take place only after the first.
cell phone with Android was released, and this time the applications would be tested in
a real device, and no longer in an emulator.
When this book was being written, the first part was already finished and
All the winners could be seen at the following address. We even have a Brazilian.
who was one of the winners with the TeraDesk application.
[Link]
1.8 Android Market
To assist in the distribution of Android applications, in addition to the promotion of your
a new platform, the Android Market website was created.
[Link]
26 Google Android
The purpose of the site is to provide application developers a co-
to make your applications available. It works like the YouTube website: just
register and submit your application. The site also has the same scheme of
comments and ratings that YouTube contains.
To publish an application, the developer needs to pay a fee of $25 and
agree to the terms of use. After that, the app can be installed.
by users. There are applications that are free, while others are paid.
Good news for developers is that 70% of profits from the apps
sold will be passed on to those who built them.
For users to use the Android Market, it is very simple. Just access the
Go to the site with your cell phone and download the desired application. The comments and the
user-generated ratings will be used to create the ranking with the
best apps.
Figure 1.1 shows the user selecting an application to be installed.
through the Android Market.
Figure 1.1 - Selection of an application in the Android Market.
Figure 1.2 shows the applications that have already been installed and are still with the classification system.
If you want to see in practice how the Android Market works, search for the
videos on YouTube, which contain great demonstrations.
Chapter■1 Introduction to Android 27
Figure 1.2 – Applications installed from the Android Market.
1.9 T-Mobile G1
The T-Mobile G1 developed by HTC was the first mobile phone released with the platform
the shape of Android and, as expected, caused a big stir in the market. The news
Since its launch, it caused a great impact and exceeded sales expectations.
from HTC, and even before its launch, all stock had already been sold out
for pre-order requests.
The first HTC G1 phones (Figure 1.3) began to be sold in the United States.
two United on October 22, 2008 for US$179, providing a screen
touch-sensitive, a Qwerty keyboard that opens downward for easier typing
and all the other features that Android has to offer.
For more details, visit the official HTC G1 website:[Link]
If you want to play a little with the G1, you can access an online emulator.
which was made available by HTC at this address:
[Link]
On this site, when you click on the menuEmulatoryou can play with the cell phone emulator
and access all your options, such as browser, maps, contacts list, etc. If
click on the menu360 Viewyou can literally hold a G1 phone in your hand and
turn it to get an idea of its appearance.
It's worth playing a little with the emulator and watching videos on YouTube.
about the G1, which demonstrates the functionalities and all the usability of Android.
28 Google Android
Figure 1.3 - Emulator of the HTC G1.
1.10 Google Nexus One
At the time this book was being written, Google had just launched its
Nexus One cellular, with Android 2.1 and a 1 GHz processor, being the fastest.
of all the Android smartphones released up to that moment.
But the great triumph of Android is its open and flexible platform, which allows
that the entire operating system be customized. Thus, several manufacturers
cell phones like Motorola, HTC, Sony Ericsson, etc. were already customizing
your Android phone models to the latest version. Customization varies from
manufacturer to manufacturer. In HTC, customization is called HTC Sense.
it completely changes the look of the device, and also adds several applications. The
Motorola calls its MotoBlur, and it is focused on social networks. The customization of
Sony Ericsson is known as UX 'User eXperience'.
With the great popularization of Android, users will be able to choose the
models that please you the most and the developers will be able to enjoy your
development platform to create amazing mobile applications.
1.11 Development of applications with Eclipse and Java
For the general joy of the nation, it is possible to develop an application for Android.
using the Java language in your preferred development environment, such as
Eclipse, Netbeans or IntelliJ IDEA.
The Eclipse is the preferred development environment by Google, and there is a plug-
in ADT (Android Development Tools) to facilitate development,
Chapter■1 Introduction to Android 29
the tests and the compilation of the project and therefore we will adopt Eclipse for our
examples.
Using the ADT plug-in it is possible to run the Android emulator directly
in Eclipse, taking advantage of all its features such as step-by-step debugging,
that works perfectly. It is also possible to control directly from Eclipse
the emulator, visualizing logs and simulating the sending of an SMS message or
a phone call, in addition to the ability to view and send files to the
emulator, run the garbage collector, view the heap memory, etc.
To distribute an Android application, it is necessary to compile the Java project into
a file with [Link](Android Package File), which is a binary file with-
agreed with the compiled classes and the project's resources. The good news is that when
compile the project in Eclipse this file is automatically created by the plug-in.
To run an application on the emulator, it is also necessary to install this.
[Link] like on a real cell phone. However, this
it is also done automatically by Eclipse when the application is executed.
But enough theory. Let's move on to the next chapter, where we will learn.
to install the SDK and the ADT plug-in for Eclipse and we will create our first app
action for Android.