0 Members and 1 Guest are viewing this topic.
Part I. Development Kit Walk-Through1. Getting to Know Android . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3Why Android? 3The Open Handset Alliance 4The Android Execution Environment 5Components of an Android Application 6Android Activity Lifecycle 8Android Service Lifecycle 10How This Book Fits Together 102. Setting Up Your Android Development Environment . . . . . . . . . . . . . . . . . . . . . . . . . 13Setting Up Your Development Environment 13Creating an Android Development Environment 14Hello, Android 18Where We’re Going 18Starting a New Android Application: HelloWorld 18Writing HelloWorld 22Running HelloWorld 243. Using the Android Development Environment for Real Applications . . . . . . . . . . . . 27MicroJobs: This Book’s Main Sample Application 27Android and Social Networking 27Downloading the MJAndroid Code 30A Brief Tour of the MJAndroid Code 30The Project Root Folder (MJAndroid) 30The Source Folder (src) 31The Resource Folder (res) 32First Steps: Building and Running the MicroJobs Application 33iiiA Very Short Tour of the Android SDK/Eclipse IDE 33Loading and Starting the Application 35Digging a Little Deeper: What Can Go Wrong? 36Running an Application on the T-Mobile Phone 39Summary 414. Under the Covers: Startup Code and Resources in the MJAndroid Application . . . . 43Initialization Parameters in AndroidManifest.xml 44Initialization in MicroJobs.java 46More Initialization of MicroJobs.java 52Summary 565. Debugging Android Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57The Tools 57Eclipse Java Editor 58Java Errors 58The Debugger 64Logcat 67Android Debug Bridge (adb) 71DDMS: Dalvik Debug Monitor Service 74Traceview 75Summary 806. The ApiDemos Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81Application Setup in the Manifest File 81Finding the Source to an Interesting Example 83Custom Title Demo 83Linkify Demo 84Adding Your Own Examples to ApiDemos 847. Signing and Publishing Your Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87Test Your Application 88Attach an End User License Agreement If Desired 89Create and Attach an Icon and Label 89Clean Up for Release 90Version Your Application 90Obtaining a Signing Certificate and API Key 90Getting a Signing Certificate for an Application You Are Going to Ship 91Getting a Signing Certificate While Debugging 93Signing Your Application 95Retesting Your Application 96Publishing on Android Market 96Signing Up As an Android Developer 96iv | Table of ContentsUploading Your Application 96Part II. Programming Topics8. Persistent Data Storage: SQLite Databases and Content Providers . . . . . . . . . . . . . 101Databases 101Basic Structure of the MicroJobsDatabase Class 102Reading Data from the Database 107Modifying the Database 110Content Providers 114Introducing NotePad 116Content Providers 118Consuming a Content Provider 1299. Location and Mapping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137Location-Based Services 137Mapping 139The Google Maps Activity 139The MapView and MapActivity 140Working with MapViews 140MapView and MyLocationOverlay Initialization 141Pausing and Resuming a MapActivity 144Controlling the Map with Menu Buttons 145Controlling the Map with the KeyPad 147Location Without Maps 148The Manifest and Layout Files 148Connecting to a Location Provider and Getting Location Updates 149Updating the Emulated Location 15210. Building a View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157Android GUI Architecture 157The Model 157The View 158The Controller 159Putting It Together 159Assembling a Graphical Interface 161Wiring Up the Controller 166Listening to the Model 168Listening for Touch Events 173Listening for Key Events 176Alternative Ways to Handle Events 177Advanced Wiring: Focus and Threading 179Table of Contents | vThe Menu 18311. A Widget Bestiary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187Android Views 188TextView and EditText 188Button and ImageButton 191Adapters and AdapterViews 192CheckBoxes, RadioButtons, and Spinners 193ViewGroups 198Gallery and GridView 198ListView and ListActivity 202ScrollView 204TabHost 205Layouts 208Frame Layout 209LinearLayout 209TableLayout 213AbsoluteLayout 215RelativeLayout 21612. Drawing 2D and 3D Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221Rolling Your Own Widgets 221Layout 222Canvas Drawing 226Drawables 237Bitmaps 242Bling 243Shadows, Gradients, and Filters 246Animation 247OpenGL Graphics 25213. Inter-Process Communication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257Intents: Simple, Low-Overhead IPC 258Intent Objects Used in Inter-Process Communication 258Activity Objects and Navigating the User Interface Hierarchy 259Example: An Intent to Pick How We Say “Hello World” 259Getting a Result via Inter-Process Communication 262Remote Methods and AIDL 265Android Interface Definition Language 266Classes Underlying AIDL-Generated Interfaces 270Publishing an Interface 273Android IPC Compared with Java Native Interface (JNI) 274What Binder Doesn’t Do 275vi | Table of ContentsBinder and Linux 27514. Simple Phone Calls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277Quick and Easy Phone Calls 277Creating an Example Application to Run the call Method 278Embedding the Code Snippet in a Simple Application 279Exploring the Phone Code Through the Debugger 280Creating an Instance of an Intent 282Adding Data to an Instance of an Intent 283Initiating a Phone Call 284Exception Handling 284Android Application-Level Modularity and Telephony 28515. Telephony State Information and Android Telephony Classes . . . . . . . . . . . . . . . . 287Operations Offered by the android.telephony Package 287Package Summary 288Limitations on What Applications Can Do with the Phone 288Example: Determining the State of a Call 289Android Telephony Internals 291Inter-Process Communication and AIDL in theandroid.internal.telephony Package 291The android.internal.telephony Package 292The android.internal.telephony.gsm Package 295Exploring Android Telephony Internals 299Android and VoIP 302Appendix: Wireless Protocols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 309