1、 1 lder namedsrc. This is the source code folder for the given sample application. Youcan use this to view, edit,and recompile the code for any of the applications. Takeadvantage of this source code to learn some tricks and tips about the Android platform. Android Tools The Android SDK supplies deve
2、lopers with a number of powerful and useful tools.Throughoutthis book, you will use only a handful of them directly. This section takes aquick look at just afew of these tools, which will be covered in much more depth in thefollowing chapters, as youdive into command-line development. NOTE For more
3、detailed information about the other tools included in the Android SDK,consult the Android doc files. emulator.exe Arguably one of the most important tools included in the Android SDK is emulator.exe.emulator.exe launches the Android Emulator. The Android Emulator is used toyoure yourapplications in
4、 a pseudo-Android environment. Given that, as of the writing of this book,therewere no hardware devices yet released for the Android platform, emulator.exe isgoing to be your only means to test applications on a “native” platform. You can run emulator.exe from the command line or execute it from wit
5、hin Eclipse.In this book, youll usually let Eclipse launch the Android Emulator environment for you.However, in theinterest of giving you all the information you need to program with theAndroid SDK outside of Eclipse, Chapter 6 covers command-line usage of emulator.exewhen you create your Hello Worl
6、d! applications. When using the Android Emulator to test your applications, you have two choices fornavigatingthe user interface. First, the emulator comes with usable buttons, as shown inFigure 4-1. You can use these buttons to navigate Android and any applications that youdevelop for the platform.
7、 TIP The Power On/Off, Volume Up, and Volume Down buttons are slightly hidden to thesides of the virtual device. They identify themselves when you hover the mouse pointerover them.Given that many higher-end phones now include a touch screen, the second inputchoice you have when using the emulator is
8、 a simulated touch screen. You use yourmouse as a stylus. The objects on the emulators screen can be interacted with usingthe mouse. adb.exe Another tool that will become very useful to you when you are using command-lineprogrammingis Android Debug Bridge or adb (adb.exe). This tool allows you to is
9、suecommands to the Emulator.exe tool. When you are working in a command-lineenvironment, the adb tool allows you to do the following: Start and stop the server Install and uninstall applications Move files to and from the emulator MKSDCARD.exe MKSDCARD.exe is a very useful tool if you are testing an
10、 application that will needto read orwrite files to or from an SD Memory Card inserted into the mobile device.MKSDCARD.execreates a small partition drive on your drive that will hold and retainthe 2 test files. The emulatorwill treat this partition like an SD Memory Card. DX.exe DX.exe is the compil
11、er of the Android SDK. When run against your Java files, DX.exewill createfiles with .dex extensionsDalvik executable format. These files are in thecorrect format to beunderstood by, and run on, an Android device. NOTE Android executable files are called Dalvik executable files as a reference to the
12、 Dalvikvirtual machine that Android used to run all applications. The Dalvik virtual machineruns each application in its own thread with the same priority as core Androidapplications. activityCreator(.bat or .pn) activityCreator is a simple command-line tool that is used to set up a basic developmen
13、tenvironment. When run from the command line, activityCreator will set up the shell filesneeded to create a basic Android application. Having these shell files is especially usefulif you are not using Eclipse. The Android plugin for Eclipse sets up these shell files foryou by calling the activityCre
14、ator when you create a new project.Depending on what type of environment you are running, you will see theactivityCreator represented by a different type of script file. If you are in a Windowsenvironment, this will be a .bat file; otherwise it will be a python (.pn) script. Yousimply execute the sc
15、ript, which in turn calls the actual activityCreator process withthe correct parameters. APIs The API, or application programming interface, is the core of the Android SDK. An APIis a collection of functions, methods, properties, classes, and libraries that is used byapplication developers to create
16、 programs that work on specific platforms. The AndroidAPI contains all thespecific information that you need to create applications that canwork on and interact with an Android-based application. The Android SDK also contains two supplementary sets of APIsthe Google APIsand the Optional APIs. Subseq
17、uent chapters will focus much more on these APIs as youbegin writingapplications that utilize them. For now, take a quick look at what theyinclude so that you are familiar with their uses. Google API s The Google APIs are included in the Android SDK and contain the programmingreferences that allow y
18、ou to tie your applications into existing Google services. If youare writing an Androidapplication and want to allow your user to access Google servicesthrough your application, you need to include the Google API. Located in the android.jar file, the Google API is contained within the com.google.*pa
19、ckage. There are quite a few packages that are included with the Google API. Some ofthe packages that are shipped in the Google API include those for graphics, portability,contacts, and calendar utilities. However, the packages devoted to Google Maps will bethe primary focus in this book. Using the
20、com.google.android.maps package, which contains information for GoogleMaps, you can create applications that interact seamlessly with the already familiarinterface of Google Maps. This one set of packages opens a whole world of usefulapplications just waiting to be created. The Google API also conta
21、ins a useful set of packages that allows you to takeadvantage of the newer Extensible Messaging and Presence Protocol (XMPP) developedby the Jabber open source community. Using XMPP, applications can quickly becomeaware of other clients presence and 3 availability for the purpose of messaging andcom
22、munications. The API packages dealing with XMPP are very useful if you wantto create a “chat”-style program that utilizes the phone messaging capabilities. Optional APIs The Android SDK includes a number of Optional APIs that cover functionality notcovered by the standard Android APIs. These Optiona
23、l APIs are considered optionalbecause they deal with functionality that may or may not be present on a given handsetdevice. That is, some devices created for the Android platform may include upgradesand features that others do not; the Optional APIs cover your programming optionswhen trying to utili
24、ze these features in your Android applications. One of these optional features (which you will use later in the book) is a cell-phone-basedGPS. The Android LBS (Location-Based Services) API deals with the functionalityneeded to receive and utilize information from a devices GPS unit. (Combine theinf
25、ormation in the Android LBS API with that in the Google Maps API, and you mighthave a very useful application that can automatically display a map of where you arelocated at any given point in time.) Other Optional APIs include those for utilizing Bluetooth, Wi-Fi, playing MP3s, andaccessing 3-DOpen
26、GL-enable hardware. Application Life Cycle If you have a decent amount of experience as an application developer, you are familiarwith theconcept of an application life cycle. An application life cycle consists of thesteps that theapplications processes must follow from execution to termination. Eve
27、ryapplication, regardless of the language it was written in, has a specific life cycle, andAndroid applications are no exception. This section examines the life cycle of an ASPapplication and compares that to an Android applications life cycle. Standard ASP Application Life Cycle The life cycle of a
28、 standard ASP application is similar enough to that of an Androidapplication to make this a good comparison. ASP applications step through fivedistinct processes from launch to disposal. These steps are required to be implementedby all ASP applications, and really define what an ASP application is.
29、The steps, inorder, are 1. Application_Start 2. Event 3. HTTPApplication.Init 4. Disposal 5. Application_End Chapter 4: Exploring the Android SDK 49 50 Android: A Programmers Guide TIP Some ASP reference materials consider Disposal and Application_End to be one stepin the life cycle. However, the Di
30、sposal call can be intercepted before it is passed toApplication_End. This can allow the application to perform specific functions before it is actually destroyed. Application_Start is called when the application is requested from the server. Thisprocess in turn leads into the Event process(es). When all associated application moduleshave loaded, HTTPApplication.Init is called. The application executes its events, andwhen the user attempts to