You are on page 1of 17

Android

Android is a new open source software toolkit for mobile phones that was created by Google and the Open Handset Alliance. In a few years, its expected to be found in millions of cell phones and other mobile de ices, making Android a ma!or platform for application de elopers.

What Makes Android Special?


"here are already many mobile platforms on the market today, including #ymbian, i$hone, %indows &obile, 'lack'erry, (a a &obile )dition, *inux &obile +*i&o,, and more. %hen I tell people about Android, their first -uestion is often, why do we need another mobile standard. Android is the first en ironment that combines the following/ A truly open, free development platform based on Linux and open source: Handset makers like it because they can use and customi0e the platform without paying a royalty. 1e elopers like it because they know that the platform 2has legs3 and is not locked into any one endor that may go under or be ac-uired. A component based architecture inspired by !nternet smashups": $arts of one application can be used in another in ways not originally en isioned by the de eloper. 4ou can e en replace built5in components with your own impro ed ersions. "his will unleash a new round of creati ity in the mobile space. #ons of built in services out of the box: *ocation5based ser ices use G$# or cell tower triangulation to let you customi0e the user experience depending on where you are. A full5powered #6* database lets you harness the power of local storage for occasionally connected computing and synchroni0ation. 'rowser and map iews can be embedded directly in your applications. All these built5in capabilities help raise the bar on functionality while lowering your de elopment costs. Automatic mana$ement of the application life cycle: $rograms are isolated from each other by multiple layers of security, which will pro ide a le el of system stability not seen before in smart phones. "he end user will no longer ha e to worry about what applications are acti e or close some programs so that others can run. Android is

optimi0ed for low5power, low5memory de ices in a fundamental way that no pre ious platform has attempted. %i$h &uality $raphics and sound: #mooth, ant aliased 71 ector graphics and animation inspired by 8lash are melded with 91 accelerated OpenG* graphics to enable new kinds of games and business applications. :odecs for the most common industry standard audio and ideo formats are built right in, including H.7;<, &$9, and AA:. 'ortability across a (ide ran$e of current and future hard(are: All your programs are written in (a a and executed by Androids 1al ik irtual machine, so your code will be portable across A=&, x>;, and other architectures. #upport for a ariety of input methods is included such as keyboard, touch, and trackball. ?ser interfaces can be customi0ed for any screen resolution and Orientation. Android offers a fresh take on the way mobile applications interact with users, along with the technical underpinnings to make it possible. 'ut the best part of Android is the software that you are going to write for it. "his book will help you get off to a great start.

An )pen 'latform for Mobile *evelopment


Android is made up of se eral necessary and dependent parts including the following/ A hardware reference design that describes the capabilities re-uired of a mobile de ice in order to support the software stack A *inux operating system kernel that pro ides the low5le el interface with the hardware, memory management, and process control, all optimi0ed for mobile de ices Open source libraries for application de elopment including #6*ite, %eb@it, OpenG*, and a media manager A run time used to execute and host Android applications, including the 1al ik irtual machine and the core libraries that pro ide Android specifi c functionality. "he run time is designed to be small and effi cient for use on mobile de ices. An application framework that agnostically exposes system ser ices to the application layer, including the window manager, content pro iders, location manager, telephony, and peer5to5peer ser ices A user interface framework used to host and launch applications

$reinstalled applications shipped as part of the stack A software de elopment kit used to create applications, including the tools, plug5 ins, and documentation.

At this stage, not the entire Android stack has been released as open source, although this is expected to happen by the time phones are released to market. Its also worth noting that the applications you de elop for Android do not ha e to be open source. %hat really makes android compelling is its open philosophy, which ensures that any deficiencies in user interface or nati e application design can be fixed by writing an extension or replacement. Android pro ides you, as a de eloper, the opportunity to create mobile phone interfaces and applications designed to look, feel, and function exactly as you image them.

)pen %andset Alliance


"he )pen %andset Alliance +)%A, is a consortium of >< firms to de elop open standards for mobile de ices. &ember firms include Google, H":, #ony, 1ell, Intel, &otorola, 6ualcomm, "exas Instruments, #amsung )lectronics, *G )lectronics, "5&obile, #print Aextel, A idia, and %ind =i er #ystems. "he OHA was established on ; Ao ember 7BBC, led by Google with 9< members including mobile handset makers, application de elopers, some mobile carriers and chip makers.D9E Android, the flagship software of the alliance, is based on an open source license and competes against mobile platforms from Apple, &icrosoft, Aokia +#ymbian,, H$ +formerly $alm,, and #amsung )lectronics +bada,. Products At the same time as the announcement of the formation of the Open Handset Alliance on Ao ember F, 7BBC, the OHA also un eiled Android, an open source mobile phone platform based on the *inux kernel. An early look at the #1@ was released to de elopers on G7 Ao ember 7BBC. "he first commercially a ailable phone running Android was the H": 1ream +also known as the "5&obile GG,. It was appro ed by the 8ederal :ommunications :ommission +8::, on G> August 7BB>, and became a ailable on 77 October.

Members "he members of the Open Handset Alliance are/


+oinin$ date Mobile operators Soft(are companies Ascender :orporation @11I :orporation A"" 1o:o&o #print Aextel .oundin$ members "5&obile :hina &obile "elecom Italia "elefHnica e'ay Google *i ingImage &yriad Auance :ommunicati ons $acketIideo #ky$op #OAiIOJ Aplix Aoser )ngineering "he Astonishing "ribe %ind =i er #ystems ,ommerciali-atio n companies Semiconductor companies Audience 'roadcom :orporation H": :#= $lc. +!oined as #i=8, Intel :orporation &ar ell "echnology Group A idia :orporation 6ualcomm #ynaptics "exas Instruments A#?#"ek A@& #emiconductor A=& *ecember /001 Iodafone #oftbank 'or-s "eleca Atheros :ommunications #"5)ricsson +!oined as )ricsson &obile $latforms, #ony &obile :ommunications +!oined as #ony )ricsson, "oshiba 1ell May2+une /003 September /003 Garmin Huawei "echnologies #amsung )lectronics *G #ony &otorola &obility +!oined as &otorola, %andset manufacturers

:hina ?nicom

#IOJ

Acer

&I$# "echnologies

+anuary /040

#asken :ommunication "echnologies *imited

K") :orporation

May /040

AJ$ #oftware

+uly /040 5ovember /040

Access

&edia"ek

Iisual On

Alcatel &obile $hones :ompal :ommunications 8oxconn Haier @yocera *eno o &obile :ommunication "echnology *td. A): #harp :orporation #aygus :orporation

:hina "elecommunicatio ns :orporation 6nkno(n "elus 'ouygues "elecom

Accenture :ooliris &O"O4A :o., *td. O&=OA *L" Infotech #6*#tar International Inc. %ipro "echnologies

:ypress #emiconductor :orporation 8reescale #emiconductor Gemalto =enesas )lectronics :orporation Iia "elecom

Android version history Android .eatures


Ao licensing, distribution, or de elopment fees

%i58i hardware access G#&, )1G), and 9G networks for telephony or data transfer, allowing you to make or recei e calls or #&# messages, or to send and retrie e data across mobile networks

:omprehensi e A$Is for location5based ser ices such as G$# 8ull multimedia hardware control including playback and recording using the camera and microphone A$Is for accelerometer and compass hardware I$: message passing #hared data stores An integrated open source %eb@it5based browser 8ull support for applications that integrate &ap controls as part of their user interface $eer5to5peer +$7$, support using Google "alk &obile5optimi0ed hardware5accelerated graphics including a path5based 71 graphics library and support for 91 graphics using OpenG* )# &edia libraries for playing and recording a ariety of audioM ideo or still image formats An application framework that encourages reuse of application components and the replacement of nati e applications

5ative Android Applications


Android phones will normally come with a suite of preinstalled applications including, but not limited to/ An e5mail client compatible with Gmail but not limited to it An #&# management application A full $I& +personal information management, suite including a calendar and contacts list, both tightly integrated with Googles online ser ices A fully featured mobile Google &aps application including #treetIiew, business finder, dri ing directions, satellite iew, and traffic conditions

A %eb@it5based web browser An Instant &essaging :lient A music player and picture iewer "he Android &arketplace client for downloading third party Android applications. "he Ama0on &$9 store client for purchasing 1=& free music.

All the nati e applications are written in (a a using the Android #1@ and are run on 1al ik. Access to %ard(are includin$ ,amera, 7'S, and Accelerometer Android includes A$I libraries to simplify de elopment in ol ing the de ice hardware. "hese ensure that you dont need to create specific implementations of your software for different de ices, so you can create Android applications that work as expected on any de ice that supports the Android software stack. and power management. 5ative 7oo$le Maps, 7eo codin$, and Location 8ased Services Aati e map support lets you create a range of map5based applications that le erage the mobility of Android de ices. Android lets you create acti ities that include interacti e Google &aps as part of your user interface with full access to maps that you can control programmatically and annotate using Androids rich graphics library. Androids location5based ser ices manage technologies like G$# and Googles G#& cell5based location technology to determine the de ices current position. "hese ser ices enforce an abstraction from specific location detecting technology and let you specify minimum re-uirements +e.g., accuracy or cost, rather than choosing a particular technology. It also means that your location5based applications will work no matter what technology the host handset supports. "o combine maps with locations, Android includes an A$I for forward and re erse geo coding that lets you find map coordinates for an address, and the address of a map position. 8ack$round Services Android supports applications and ser ices designed to run in isibly in the background. &odern mobiles are by nature multifunction de icesN howe er, their limited screen si0e means that generally only one interacti e application can be isible at any time. $latforms that dont "he Android #1@ includes A$Is for location based hardware, camera, network connections, %i58i, 'luetooth, accelerometers, touch screen,

support background execution limit the iability of applications that dont need your constant attention. 'ackground ser ices make it possible to create in isible application components that perform automatic processing without direct user action. 'ackground execution allows your applications to become e ent dri en and to support regular updates, which is perfect for monitoring game scores or market prices, generating location5based alerts, or prioriti0ing and pre5screening incoming calls and #&# messages. S9Lite *atabase for *ata Stora$e and :etrieval =apid and efficient data storage and retrie al are essential for a de ice whose storage capacity is limited by its compact nature. Android pro ides a lightweight relational database for each application using #6*ite. 4our applications can take ad antage of the managed relational database engine to store data securely and efficiently. 'y default, each application database is sandboxed its content is a ailable only to the application that created it but :ontent $ro iders supply a mechanism for the managed sharing of these application databases. Shared *ata and !nter application ,ommunication Android includes three techni-ues for transmitting information from your applications for use elsewhere/ 5otifications, !ntents, and ,ontent 'roviders; 5otifications are the standard ways in which a mobile de ice traditionally alerts users. ?sing the A$I, you can trigger audible alerts, cause ibration, and fl ash the de ices *)1, as well as control status bar notification icons. !ntents pro ide a mechanism for message passing within and between applications. ?sing Intents, you can broadcast a desired action +such as dialing the phone or editing a contact, system5wide for other. ,ontent 'roviders are a way to gi e managed access to your applications pri ate database. "he data stores for nati e applications, such as the :ontact &anager, are exposed as :ontent $ro iders so you can create your own applications that read or modify these data stores. '/' Services (ith 7oo$le #alk 'ased on earlier #1@ ersions, its expected that in later releases you will once again be able to send structured messages from your application to any other Android mobile using Androids peer5to5peer +$7$, communications ser ice. "he Android $7$ ser ice uses a speciali0ed ersion of J&$$ +)xtensible &essaging and $resence $rotocol,. 'ased on Googles Google "alk instant messaging ser ice, it creates a persistent socket connection

between your de ice and any other online Android handset that ensures communication with low latency and rapid response times. %hen made a ailable, youll be able to use the Google "alk ser ice for con entional instant messaging, or an interface to send data between application instances on separate de ices. "his is strong sauce for creating interacti e applications that in ol e multiple users, such as real5time multiplayer games or social applications. "he $7$ ser ice also offers presence notification, which is used to see if a contact is online. %hile the $7$ ser ice is ery attracti e in itself, it also plays ery well with other Android features. Imagine a background ser ice that transmits locations between friends and a corresponding mapping application that displays these locations or alerts you when friends are nearby. Owing to security concerns, sending data messages with Google "alk isnt possible in Android G.B. An instant messaging client is a ailable, and its expected that J&$$5compatible I& and data messaging will be made a ailable to de elopers in a future #1@ release. <xtensive Media Support and /*=>* 7raphics 'igger screens and brighter, higher5resolution displays ha e helped make mobiles multimedia de ices. "o make the most of the hardware a ailable, Android pro ides graphics libraries for 71 can as drawing and 91 graphics with OpenG*. Android also offers comprehensi e libraries for handling still images, ideo, and audio files including the &$)G<, H.7;<, &$9, AA:, A&=, ($G, $AG, and GI8 formats. )ptimi-ed Memory and 'rocess Mana$ement Androids process and memory management is a little unusual. *ike (a a and .A)", Android uses its own run time and irtual machine to manage application memory. ?nlike either of these frameworks, the Android run time also manages the process lifetimes. Android ensures application responsi eness by stopping and killing processes as necessary to free resources for higher5priority applications. In this context, priority is determined depending on the application with which the user is interacting. )nsuring that your applications are prepared for a swift death but are still able to remain responsi e and update or restart in the background if necessary, is an important consideration in an en ironment that does not allow applications to control their own lifetimes.

*alvik ?irtual Machine

It is optimi0ed for low memory re-uirements, and is designed to allow multiple I& instances to run at once, relying on the underlying operating system for process isolation, memory management and threading support. 1al ik is often referred to as a (a a Iirtual &achine, but this is not strictly accurate, as the byte code on which it operates is not (a a byte code. Instead, a tool named dx, included in the Android #1@, transforms the (a a :lass files of (a a classes compiled by a regular (a a compiler into another class file format +the .dex format,. 1al ik is the name of the Iirtual &achine in which Android applications are run. "his I& executes 1al ik byte code, which is compiled from programs written in the (a a language. Aote that the 1al ik I& is not a (a a I& +(I&,. ) ery Android application runs in its own process, with its own instance of the 1al ik irtual machine. At boot time, a single irtual machine, called O0ygoteO is created, which preloads a long list of classes. All other P!a aP programs or ser ices are forked from this process, and run as their own process +and threads, in their own address space. 'oth applications and system ser ices in the Android framework are implemented in P!a aP. 1al ik was written so that a de ice can run multiple I&s efficiently. "he 1al ik I& executes code in the 1al ik )xecutable +.dex, format which is optimi0ed for minimal memory footprint. "he I& is register5based, and runs classes compiled by a (a a language compiler that ha e been transformed into the .dex format by the included PdxP tool. &ost Android applications are deli ered and stored on the system as packages +.apk,, which include both dex bytes code and resources. 1uring first boot5up the system creates a cache of dex classes in MdataMdal ik5cache.

*ex .ile .ormat


Android programs are compiled into ;dex +1al ik )xecutable, files, which are in turn 0ipped into a single ;apk +Android $ackage, file on the de ice. .dex files can be created by automatically translating compiled applications written in the (a a programming language.

Android Architecture

"he software stack is split into 8our *ayers// "he application layer "he application framework "he libraries and runtime *inux @ernel

Applications and Wid$ets


"he highest layer in the Android architecture diagram is the Applications and %idgets layer. "hink of this as the tip of the Android iceberg. )nd users will see only these programs, blissfully unaware of all the action going on below the waterline. As an Android de eloper, howe er, you know better. Applications are programs that can take o er the whole screen and interact with the user. On the other hand, %idgets +sometimes called gadgets,, only operate in a small rectangle of the Home screen application. "he ma!ority of this book will co er application de elopment, because thats what most of you will be writing.

%hen someone buys an Android phone, it will come prepackaged with a number of standard system applications, including the following/ 'hone dialer <mail ,ontacts Web bro(ser Android Market ?sing the Android &arket, the user will be able to download new programs to run on their phone. "hats where you come in. 'y the time you finish this book, youll be able to write your own killer applications for Android. Aow lets take a closer look at the life cycle of an Android application. Its a little different from what youre used to seeing.

Application .rame(ork
Access location information, run background ser ices, set alarms, add notifications to the status bar, and much, much more. 'uild an application, including lists, grids, text boxes, buttons, and e en an embeddable web browser. as locali0ed strings, graphics, and layout files "he nati e libraries and runtime, youll find the Application 8ramework layer. "his layer pro ides the high5le el building blocks you will use to create your applications. "he framework comes preinstalled with Android, but you can also extend it with your own components as needed. "he most important parts of the framework are as follows/ Activity mana$er/ "his controls the life cycle of applications and maintains a common 2back stack3 for user na igation. ,ontent providers: "hese ob!ects encapsulate data that needs to be shared between applications, such as contacts. :esource mana$er: =esources are anything that goes with your program that is not code. $ro iding access to non5code resources such

Location mana$er/ An Android phone always knows where it is. 5otification mana$er/ ) ents such as arri ing messages, appointments, proximity alerts, alien in asions, and more can be presented in an unobtrusi e fashion to the user.

Android Libraries
Android offers a number of A$Is for de eloping your applications. "he following list of core A$Is should pro ide an insight into whats a ailableN all Android de ices will offer support for at least these A$Is/ android;util :"he core utility package contains low5le el classes like speciali0ed containers, string formatters, and J&* parsing utilities. android;os: "he operating system package pro ides access to basic operating system ser ices like message passing, interprocess communication, clock functions, and debugging. android;$raphics: "he graphics A$I supplies the low5le el graphics classes that support can ases, colors, and drawing primiti es, and lets you draw on can ases. android;text: "he text processing tools for displaying and parsing text. android;database: #upplies the low5le el classes re-uired for handling cursors when working with databases. android;content: "he content A$I is used to manage data access and publishing by pro iding ser ices for dealing with resources, content pro iders, and packages. android;vie(: Iiews are the core user interface class. All user interface elements are constructed using a series of Iiews to pro ide the user interaction components. android;(id$et: 'uilt on the Iiew package, the widget classes are the 2heres one we created earlier3 user5interface elements for you to use in your applications. "hey include lists, buttons, and layouts. com;$oo$le;android;maps: A high5le el A$I that pro ides access to nati e map controls that you can use within your application. Includes the &apIiew control as well as the O erlay and &ap:ontroller classes used to annotate and control your embedded maps.

android;app: A high5le el package that pro ides access to the application model. "he application package includes the Acti ity and #er ice A$Is that form the basis for all your Android applications.

android;provider: "o ease de eloper access to certain standard :ontent $ro iders +such as the contacts database,, the $ro ider package offers classes to pro ide access to standard databases included in all Android distributions.

android;telephony: "he telephony A$Is gi e you the ability to directly interact with the de ices phone stack, letting you make, recei e, and monitor phone calls, phone status, and #&# messages.

android;(ebkit: "he %eb@it package features A$Is for working with %eb5 based content, including a %ebIiew control for embedding browsers in your acti ities and a cookie manager. In addition to the Android A$Is, the Android stack includes a set of :M:QQ libraries that are exposed through the application framework. "hese libraries include/

)pen7L: "he library used to support 91 graphics based on the Open G* )# G.B A$I .ree#ype: #upport for bitmap and ector font rendering S7L: "he core library used to pro ide a 71 graphics engine libc: "he standard : library optimi0ed for *inux5based embedded de ices S9Lite: "he lightweight relation database engine used to store application data SSL: #upport for using the #ecure #ockets *ayer cryptographic protocol for secure Internet communications

Advanced Android Libraries "he core libraries pro ide all the functionality you need to start creating applications for Android, but it wont be long before youre ready to del e into the ad anced A$Is that offer the really exciting functionality. Android hopes to target a wide range of mobile hardware, so be aware that the suitability and implementation of the following A$Is will ary depending on the de ice upon which they are implemented. android;location "he location5based ser ices A$I gi es your applications access to the de ices current physical location. *ocation5based ser ices pro ide generic access to location

information using whate er position5fixing hardware or technology is a ailable on the de ice. android;media "he media A$Is pro ide support for playback and recording of audio and ideo media files, including streamed media. android;open$l Android offers a powerful 91 rendering engine using the OpenG* )# A$I that you can use to create dynamic 91 user interfaces for your applications.

android;hard(are %here a ailable, the hardware A$I exposes sensor hardware including the camera, accelerometer, and compass sensors. android;bluetooth, android;net;(ifi , and android;telephony Android also pro ides low le el access to the hardware platform, including 'luetooth, %i58i, and telephony hardware.

Android :untime
Also sitting on top of the kernel is the Android runtime, including the 1al ik irtual machine and the core (a a libraries. "he 1al ik I& is Googles implementation of (a a, optimi0ed for mobile de ices. All the code you write for Android will be written in (a a and run within the I&. 1al ik differs from traditional (a a in two important ways/ #he *alvik ?M runs ;dex files, which are con erted at compile time from standard .class and .!ar files. .dex files are more compact and efficient than class files, an important consideration for the limited memory and battery5powered de ices that Android targets. #he core +ava libraries that come with Android are different from both the (a a #tandard )dition +(a a #), libraries and the (a a &obile )dition +(a a &), libraries. "here is a substantial amount of o erlap, howe er. In Appendix A, on page 7GR, youll find a comparison of Android and standard (a a libraries.

Linux @ernel
Android is built on top of a solid and pro en foundation/ the *inux kernel. :reated by *inus "or alds in GRRG while he was a student at the ?ni ersity of Helsinki, *inux can be found today in e erything from wristwatches to supercomputers. *inux pro ides the hardware

abstraction layer for Android, allowing Android to be ported to a wide ariety internallyN Android uses *inux for its memory management, process management, networking, and other operating system ser ices. "he Android phone user will ne er see *inux and your programs will not make *inux calls directly. As a de eloper, though, youll need to be aware its there. #ome utilities you need during de elopment interact with *inux. 8or example, the adb shell commandG will open a *inux shell in which you can enter other commands to run on the de ice. 8rom there you can examine the *inux file system, iew acti e processes, and so forth, sub!ect to security restrictions of platforms in the future.

Activity Lifecycle
1uring its lifetime, each acti ity of an Android program can be in one of se eral states, as shown in 8igure. 4ou, the de eloper, do not ha e control o er what state your program is in. "hats all managed by the system. Howe er, you do get notified when the state is about to change through the onJJ+, method calls. 4ou o erride these methods in your Acti ity class, and Android will call them at the appropriate time/ )n,reate A8undleB: "his is called when the acti ity first starts up. 4ou can use it to perform one5time initiali0ation such as creating the user interface. on:reate+ , takes one parameter that is either null or some state information pre iously sa ed by the on#a eInstance#tate+, method. onStartA B: "his indicates the acti ity is about to be displayed to the user. on:esumeA B: "his is called when your acti ity can start interacting with the user. "his is a good place to start animations and music.

on'auseA B: "his runs when the acti ity is about to go into the background, usually because another acti ity has been launched in front of it. "his is where you should sa e your programs persistent state, such as a database record being edited. onStopA B: "his is called when your acti ity is no longer isible to the user and it wont be needed for a while. If memory is tight, on#top+ , may ne er be called +the system may simply terminate your process,. on:estartA B: If this method is called, it indicates your acti ity is being redisplayed to the user from a stopped state. S on1estroy+ ,/ "his is called right before your acti ity is destroyed. If memory is tight, on1estroy+ , may ne er be called +the system may simply terminate your process,. onSave!nstanceStateA8undleB: Android will call this method to allow the acti ity to sa e per5instance state, such as a cursor position within a text field. ?sually you wont need to o erride it because the default implementation sa es the state for all your user interface controls automatically on:estore!nstanceStateA8undleB: "his is called when the acti ity is being reinitiali0ed from a state pre iously sa ed by the on#a eInstance#tate+ , method. "he default implementation restores the state of your user interface.

ITS ALIVE! 37

You might also like