ebook img

Apress Android Recip.. PDF

442 Pages·2011·8.14 MB·English
by  
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Apress Android Recip..

Android Recipes A Problem-Solution Approach ■ ■ ■ Dave Smith Jeff Friesen i Android Recipes: A Problem-Solution Approach Copyright © 2011 by Dave Smith and Jeff Friesen All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-3413-5 ISBN-13 (electronic): 978-1-4302-3414-2 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editor: Tom Welsh Technical Reviewer: Paul Connolly Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Corbin Collins Copy Editor: Tracy Brown Compositor: MacPS, LLC Indexer: BIM Indexing & Proofreading Services Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at www.apress.com. Contents ■Chapter 1: Getting Started with Android ......................................................... 1(cid:1) What Is Android? ....................................................................................................................................................... 1(cid:1) History of Android ..................................................................................................................................................... 2(cid:1) Android Architecture ................................................................................................................................................. 3(cid:1) App Architecture ....................................................................................................................................................... 7(cid:1) Components ......................................................................................................................................................... 7(cid:1) T Intents .................................................................................................................................................................. 9(cid:1) Manifest ............................................................................................................................................................. 11(cid:1) App Package ...................................................................................................................................................... 13(cid:1) Activities in Depth ................................................................................................................................................... 13(cid:1) Services in Depth .................................................................................................................................................... 19(cid:1) Broadcast Receivers in Depth ................................................................................................................................ 25(cid:1) Content Providers in Depth .................................................................................................................................... 26(cid:1) 1–1. Installing the Android SDK ........................................................................................................................ 27(cid:1) 1–2. Installing an Android Platform .................................................................................................................. 30(cid:1) 1–3. Creating an Android Virtual Device ........................................................................................................... 34(cid:1) 1–4. Starting the AVD ........................................................................................................................................ 36(cid:1) 1–5. Introducing UC .......................................................................................................................................... 41(cid:1) 1–6. Creating UC ................................................................................................................................................ 53(cid:1) 1–7. Installing and Running UC ......................................................................................................................... 55(cid:1) 1–8. Preparing UC for Publishing ...................................................................................................................... 59(cid:1) 1–9. Migrating to Eclipse .................................................................................................................................. 63(cid:1) 1–10. Developing UC with Eclipse .................................................................................................................... 66(cid:1) Summary ................................................................................................................................................................ 72(cid:1) ■Chapter 2: User Interface Recipes ................................................................. 75(cid:1) 2–1. Customizing the Window ......................................................................................................................... 75(cid:1) 2–2. Creating and Displaying Views ................................................................................................................ 83(cid:1) 2–3. Monitoring Click Actions .......................................................................................................................... 85(cid:1) 2–4. Resolution-Independent Assets ............................................................................................................... 86(cid:1) 2–5. Locking Activity Orientation ..................................................................................................................... 88(cid:1) 2–6. Dynamic Orientation Locking ................................................................................................................... 89(cid:1) 2–7. Manually Handling Rotation ..................................................................................................................... 91(cid:1) 2–8. Creating Pop-Up Menu Actions ................................................................................................................ 93(cid:1) 2–9. Customizing Options Menu ...................................................................................................................... 98(cid:1) 2–10. Customizing Back Button ..................................................................................................................... 101(cid:1) 2–11. Emulating the Home Button ................................................................................................................. 102(cid:1) 2–12. Monitoring TextView Changes ............................................................................................................. 103(cid:1) 2–13. Scrolling TextView Ticker .................................................................................................................... 106(cid:1) 2–14. Animating a View ................................................................................................................................. 107(cid:1) 2–15. Creating Drawables as Backgrounds ................................................................................................... 115(cid:1) 2–16. Creating Custom State Drawables ....................................................................................................... 119(cid:1) 2–17. Applying Masks to Images ................................................................................................................... 121(cid:1) 2–18. Creating Dialogs that Persist ............................................................................................................... 125(cid:1) 2–19. Implementing Situation-Specific Layouts ............................................................................................ 127(cid:1) 2–20. Customizing Keyboard Actions ............................................................................................................ 132(cid:1) 2–21. Dismissing Soft Keyboard .................................................................................................................... 135(cid:1) 2–22. Customizing AdapterView Empty Views .............................................................................................. 136(cid:1) 2–23. Customizing ListView Rows ................................................................................................................. 138(cid:1) 2–24. Making ListView Section Headers ....................................................................................................... 142(cid:1) 2–25. Creating Compound Controls ............................................................................................................... 145(cid:1) Useful Tools to Know: DroidDraw ....................................................................................................................... 148(cid:1) Obtaining and Launching DroidDraw ............................................................................................................. 149(cid:1) Exploring DroidDraw’s User Interface ............................................................................................................ 150(cid:1) Creating a Simple Screen .............................................................................................................................. 151(cid:1) Summary ............................................................................................................................................................ 153(cid:1) ■Chapter 3: Communications and Networking .............................................. 155(cid:1) 3–1. Displaying Web Information ................................................................................................................... 155(cid:1) 3–2. Intercepting WebView Events ................................................................................................................ 159(cid:1) 3–3. Accessing WebView with JavaScript ..................................................................................................... 161(cid:1) 3–4. Downloading an Image File ................................................................................................................... 163(cid:1) 3–5. Downloading Completely in the Background ......................................................................................... 166(cid:1) 3–6. Accessing a REST API ............................................................................................................................ 170(cid:1) 3–7. Parsing JSON ......................................................................................................................................... 176(cid:1) 3–8. Parsing XML ........................................................................................................................................... 180(cid:1) 3–8. Receiving SMS ....................................................................................................................................... 184(cid:1) 3–9. Sending an SMS Message ..................................................................................................................... 186(cid:1) 3–10. Communicating over Bluetooth ........................................................................................................... 188(cid:1) 3–11. Querying Network Reachability ........................................................................................................... 197(cid:1) Summary ............................................................................................................................................................ 198(cid:1) ■Chapter 4: Interacting with Device Hardware and Media ........................... 201(cid:1) 4–1. Integrating Device Location ................................................................................................................... 201(cid:1) 4–2. Mapping Locations ................................................................................................................................. 205(cid:1) 4–3. Annotating Maps .................................................................................................................................... 209(cid:1) 4–4. Capturing Images and Video .................................................................................................................. 216(cid:1) 4–5. Making a Custom Camera Overlay ......................................................................................................... 221(cid:1) 4–6. Recording Audio ..................................................................................................................................... 227(cid:1) 4–7. Adding Speech Recognition ................................................................................................................... 229(cid:1) 4–8. Playing Back Audio/Video ...................................................................................................................... 231(cid:1) 4–9. Creating a Tilt Monitor ........................................................................................................................... 240(cid:1) 4–10. Monitoring Compass Orientation ......................................................................................................... 243(cid:1) Useful Tools to Know: SensorSimulator .............................................................................................................. 247(cid:1) Obtaining Sensor Simulator ........................................................................................................................... 248(cid:1) Launching Sensor Simulator Settings and Sensor Simulator ........................................................................ 248(cid:1) Accessing Sensor Simulator from Your Apps ................................................................................................ 253(cid:1) Summary ............................................................................................................................................................ 255(cid:1) ■Chapter 5: Persisting Data .......................................................................... 257(cid:1) 5–1. Making a Preference Screen ................................................................................................................. 257(cid:1) 5–2. Persisting Simple Data .......................................................................................................................... 262(cid:1) 5–3. Reading and Writing Files ...................................................................................................................... 266(cid:1) 5–4. Using Files as Resources ....................................................................................................................... 271(cid:1) 5–5. Managing a Database ............................................................................................................................ 274(cid:1) 5–6. Querying a Database .............................................................................................................................. 279(cid:1) 5–7. Backing Up Data .................................................................................................................................... 281(cid:1) 5–8. Sharing Your Database .......................................................................................................................... 286(cid:1) 5–9. Sharing Your Other Data ........................................................................................................................ 292(cid:1) Useful Tools to Know: SQLite3 ............................................................................................................................ 298(cid:1) SQLite3 and UC .............................................................................................................................................. 300(cid:1) Summary ............................................................................................................................................................ 308(cid:1) ■Chapter 6: Interacting with the System ...................................................... 309(cid:1) 6–1. Notifying from the Background .............................................................................................................. 309(cid:1) 6–2. Creating Timed and Periodic Tasks ....................................................................................................... 313(cid:1) 6–3. Scheduling a Periodic Task ................................................................................................................... 315(cid:1) 6–4. Creating Sticky Operations .................................................................................................................... 319(cid:1) 6–5. Running Persistent Background Operations .......................................................................................... 324(cid:1) 6–6. Launching Other Applications ................................................................................................................ 330(cid:1) 6–7. Launching System Applications ............................................................................................................. 333(cid:1) 6–8. Letting Other Applications Launch Yours ............................................................................................... 338(cid:1) 6–9. Interacting with Contacts ....................................................................................................................... 340(cid:1) 6–10. Picking Device Media ........................................................................................................................... 347(cid:1) 6–11. Saving to the MediaStore .................................................................................................................... 349(cid:1) Summary ............................................................................................................................................................ 352(cid:1) ■Chapter 7: Working with Libraries .............................................................. 353(cid:1) 7–1. Creating Java Library JARs .................................................................................................................... 353(cid:1) 7–2. Using Java Library JARs ........................................................................................................................ 356(cid:1) 7–3. Creating Android Library Projects .......................................................................................................... 359(cid:1) 7–4. Using Android Library Projects .............................................................................................................. 363(cid:1) 7–5. Charting ................................................................................................................................................. 366(cid:1) 7–6. Practical Push Messaging ...................................................................................................................... 375(cid:1) Summary ............................................................................................................................................................ 384(cid:1) ■Appendix A: Scripting Layer for Android ..................................................... 385(cid:1) Installing SL4A .................................................................................................................................................... 385(cid:1) Exploring SL4A .................................................................................................................................................... 386(cid:1) Adding a Shell Script ..................................................................................................................................... 387(cid:1) Accessing the Linux Shell .............................................................................................................................. 388(cid:1) Installing the Python Interpreter ......................................................................................................................... 389(cid:1) Scripting with Python .......................................................................................................................................... 394(cid:1) ■Appendix B: Android NDK ............................................................................ 397(cid:1) Installing the NDK ............................................................................................................................................... 397(cid:1) Exploring the NDK ............................................................................................................................................... 400(cid:1) Greetings from the NDK ...................................................................................................................................... 401(cid:1) Building and Running NDKGreetings with the Android SDK ........................................................................... 403(cid:1) Building and Running NDKGreetings with Eclipse ......................................................................................... 406(cid:1) Sampling the NDK ............................................................................................................................................... 407(cid:1) ■Appendix C: App Design Guidelines ............................................................. 411(cid:1) C–1. Designing Filtered Apps ......................................................................................................................... 411(cid:1) C–2. Designing High-Performance Apps ........................................................................................................ 413(cid:1) C–3. Designing Responsive Apps .................................................................................................................. 415 C–4. Designing Seamless Apps ..................................................................................................................... 416(cid:1) Index ............................................................................................................... 419 1 1 Chapter Getting Started with Android Android is hot, and many people are developing Android applications (apps for short). Perhaps you would also like to develop apps, but are unsure about how to get started. Although you could study Google’s online Android Developer’s Guide (http://developer.android.com/guide/index.html) to acquire the needed knowledge, you might be overwhelmed by the vast amount of information that this guide presents. In contrast, this chapter provides just enough theory to help you understand the basics of Android. This theory is followed by several recipes that teach you how to develop apps and prepare them for publication to Google’s Android Market. What Is Android? The Android Developer’s Guide defines Android as a software stack – a set of software subsystems needed to deliver a fully functional solution – for mobile devices. This stack includes an operating system (a modified version of the Linux kernel), middleware (software that connects the low-level operating system to high-level apps) that’s partly based on Java, and key apps (written in Java) such as a web browser (known as Browser) and a contact manager (known as Contacts). Android offers the following features: (cid:1) Application framework enabling reuse and replacement of app components (discussed later in this chapter) (cid:1) Bluetooth, EDGE, 3G, and WiFi support (hardware dependent) (cid:1) Camera, GPS, compass, and accelerometer support (hardware dependent) (cid:1) Dalvik Virtual Machine (DVM) optimized for mobile devices (cid:1) GSM Telephony support (hardware dependent) 1 2 CHAPTER 1: Getting Started with Android (cid:1) Integrated browser based on the open source WebKit engine (cid:1) Media support for common audio, video, and still image formats (MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, GIF) (cid:1) Optimized graphics powered by a custom 2D graphics library; 3D graphics based on the OpenGL ES 1.0 specification (hardware acceleration optional) (cid:1) SQLite for structured data storage Although not part of an Android device’s software stack, Android’s rich development environment (including a device emulator and a plugin for the Eclipse IDE) could also be considered an Android feature. History of Android Contrary to what you might expect, Android did not originate with Google. Instead, Android was initially developed by Android, Inc., a small Palo Alto, California-based startup company. Google bought this company in July 2005 and released a preview version of the Android SDK in November 2007. In mid-August, 2008, Google released the Android 0.9 SDK beta, and subsequently released the Android 1.0 SDK one month later. Table 1–1 outlines subsequent SDK update releases. (Starting with version 1.5, each major release comes under a code name that’s based on a dessert item.) Table 1–1. Android Update Releases SDK Update Release Date and Changes 1.1 Google released SDK 1.1 on February 9, 2009. Changes included paid apps (via Android Market) and “search by voice” support. 1.5 (Cupcake) Google released SDK 1.5 on April 30, 2009. Changes included the ability to Based on Linux record and watch videos through camcorder mode, the ability to upload Kernel 2.6.27 videos to YouTube and pictures to Picasa, the ability to populate the home screen with widgets, and animated screen transitions. 1.6 (Donut) Google released SDK 1.6 on September 15, 2009. Changes included an Based on Linux improved Android Market experience, an integrated Kernel 2.6.29 camera/camcorder/gallery interface, updated “search by voice” with speed and other improvements, and an updated search experience. 2.0/2.1 (Eclair) Google released SDK 2.0 on October 26, 2009. Changes included a Based on Linux revamped user interface, a new contacts list, support for Microsoft Kernel 2.6.29 Exchange, digital zoom, improved Google Maps (version 3.1.2), HTML5 support for the Browser app, live wallpapers, and Bluetooth 2.1 support. Google subsequently released SDK update 2.0.1 on December 3, 2009, and SDK update 2.1 on January 12, 2010. CHAPTER 1: Getting Started with Android 3 SDK Update Release Date and Changes 2.2 (Froyo) Google released SDK 2.2 on May 20, 2009. Changes included the Based on Linux integration of Chrome’s V8 JavaScript engine into the Browser app, voice Kernel 2.6.32 dialing and contact sharing over Bluetooth, Adobe Flash 10.1 support, additional app speed improvements courtesy of a JIT implementation, and USB tethering and WiFi hotspot functionality. 2.3 (Gingerbread) Google released SDK 2.3 on December 6, 2010. Changes included a new Based on Linux concurrent garbage collector that improves an app’s responsiveness, Kernel 2.6.35.7 support for gyroscope sensing, support for WebM video playback and other video improvements, support for near field communication, and improved social networking features. This book focuses on Android 2.3. Google subsequently released SDK 2.3.1 to fix some bugs, and SDK 2.3.3, a small feature release that adds several improvements and APIs to the Android 2.3 platform. 3.0 (Honeycomb) Google released SDK 3.0 on February 22, 2011. Unlike previous releases, Based on Linux version 3.0 focuses exclusively on tablets, such as Motorola Zoom, the first 2.6.36 tablet to be released (on February 24, 2011). In addition to an improved user interface, version 3.0 improves multitasking, supports multicore processors, supports hardware acceleration, and provides a 3D desktop with redesigned widgets. Android Architecture The Android software stack consists of apps at the top, middleware (consisting of an application framework, libraries, and the Android runtime) in the middle, and a Linux kernel with various drivers at the bottom. Figure 1–1 shows this layered architecture. 4 CHAPTER 1: Getting Started with Android Figure 1–1. Android’s layered architecture consists of several major parts. Users care about apps, and Android ships with a variety of useful core apps, which include Browser, Contacts, and Phone. All apps are written in the Java programming language. Apps form the top layer of Android’s architecture. Directly beneath the app layer is the application framework, a set of high-level building blocks for creating apps. The application framework is preinstalled on Android devices and consists of the following components: (cid:1) Activity Manager: This component provides an app’s lifecycle and maintains a shared activity stack for navigating within and among apps. Both topics are discussed later in this chapter. (cid:1) Content Providers: These components encapsulate data (such as the Browser app’s bookmarks) that can be shared among apps. (cid:1) Location Manager: This component makes it possible for an Android device to be aware of its physical location.

Description:
App Architecture . 7 App Package . 385. Installing SL4A .
See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.