ebook img

The Android Developer's Collection PDF

212 Pages·2011·41.03 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 The Android Developer's Collection

The Android Developer’s Collection Upper Saddle River, NJ • Boston • Indianapolis • San Francisco New York • Toronto • Montreal • London • Munich • Paris • Madrid Cape Town • Sydney • Tokyo • Singapore • Mexico City Note from the Publisher The Android Developer’s Collection includes two highly successful Android application development eBooks: • The Android Developer’s Cookbook: Building Applications with the Android SDK • Android Wireless Application Development, Second Edition The Android Developer’s Cookbook contains many invaluable “recipes” that working devel- opers will want to use in their production-level code. Android Wireless Application Development is an excellent way for experienced developers to come up-to-speed quickly on the ins and outs of Android application development. To simplify access, we’ve appended “A” to pages of The Android Developer’s Cookbook and “B” to pages of Android Wireless Application Development.This enabled us to produce a sin- gle, comprehensive table of contents and dedicated indexes so that you can easily link to the topics you want.We hope you find this collection useful! —The editorial and production teams at Addison-Wesley Professional Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks.Where those designations appear in this book, and the pub- lisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The authors and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: U.S. Corporate and Government Sales (800) 382-3419 Table of Contents THE ANDROID DEVELOPER’S COOKBOOK 1 Overview of Android . . . . . . . . . . . . . . . . . . . . .1A The Evolution of Android . . . . . . . . . . . . . . . . . . . . .1A The Dichotomy of Android . . . . . . . . . . . . . . . . . . . .2A Devices Running Android . . . . . . . . . . . . . . . . . . . . .2A HTC Models . . . . . . . . . . . . . . . . . . . . . . . . . . . .6A Motorola Models . . . . . . . . . . . . . . . . . . . . . . . .6A Samsung Models . . . . . . . . . . . . . . . . . . . . . . . .6A Tablets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7A Other Devices . . . . . . . . . . . . . . . . . . . . . . . . . .7A Hardware Differences on Android Devices . . . . . . . .8A Screens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .8A User Input Methods . . . . . . . . . . . . . . . . . . . . . .9A Sensors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .9A Features of Android . . . . . . . . . . . . . . . . . . . . . . .10A Multiprocess and App Widgets . . . . . . . . . . . . .11A Touch, Gestures, and Multitouch . . . . . . . . . . . .11A Hard and Soft Keyboards . . . . . . . . . . . . . . . . .11A Android Development . . . . . . . . . . . . . . . . . . . . . .11A How to Use the Recipes in This Book . . . . . . . .12A Designing Applications Well . . . . . . . . . . . . . . .12A Maintaining Forward Compatibility . . . . . . . . . . .13A Robustness . . . . . . . . . . . . . . . . . . . . . . . . . . .13A Software Development Kit . . . . . . . . . . . . . . . . . . .14A Installing and Upgrading . . . . . . . . . . . . . . . . . .14A Software Features and API Level . . . . . . . . . . . .15A Emulator and Android Device Debug . . . . . . . . .16A Using the Android Debug Bridge . . . . . . . . . . . .18A Signing and Publishing . . . . . . . . . . . . . . . . . . .18A Android Market . . . . . . . . . . . . . . . . . . . . . . . . . . .19A End-User License Agreement . . . . . . . . . . . . . . .19A Improving App Visibility . . . . . . . . . . . . . . . . . . .19A Differentiating an App . . . . . . . . . . . . . . . . . . . .20A Charging for an App . . . . . . . . . . . . . . . . . . . . .20A Managing Reviews and Updates . . . . . . . . . . . .21A Alternatives to the Android Market . . . . . . . . . .22A 2 Application Basics: Activities and Intents . . . .23A Android Application Overview . . . . . . . . . . . . . . . . .23A Recipe: Creating a Project and an Activity . . . . .24A Directory Structure of Project and Autogenerated Content . . . . . . . . . . . . . . . . . .26A Android Package and Manifest File . . . . . . . . . .28A Renaming Parts of an Application . . . . . . . . . . .30A Activity Lifecycle . . . . . . . . . . . . . . . . . . . . . . . . . .30A Recipe: Utilizing Other Lifecycle Functions . . . . .31A Recipe: Forcing Single Task Mode . . . . . . . . . . .33A Recipe: Forcing Screen Orientation . . . . . . . . . .34A Recipe: Saving and Restoring Activity Information . . . . . . . . . . . . . . . . . . . . . . . . . .34A Multiple Activities . . . . . . . . . . . . . . . . . . . . . . . . .35A Recipe: Using Buttons and TextView . . . . . . . . .36A Recipe: Launching Another Activity from an Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .37A Recipe: Launching an Activity for a Result Using Speech to Text . . . . . . . . . . . . . . . . . . .41A Recipe: Implementing a List of Choices . . . . . . .43A Recipe: Using Implicit Intents for Creating an Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . .44A Recipe: Passing Primitive Data Types Between Activities . . . . . . . . . . . . . . . . . . . . .46A 3 Threads, Services, Receivers, and Alerts . . . . .51A Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51A Recipe: Launching a Secondary Thread . . . . . . .51A Recipe: Creating a Runnable Activity . . . . . . . . .55A Recipe: Setting a Thread’s Priority . . . . . . . . . . .57A Recipe: Canceling a Thread . . . . . . . . . . . . . . .57A Recipe: Sharing a Thread Between Two Applications . . . . . . . . . . . . . . . . . . . . . . . . . .58A Messages Between Threads: Handlers . . . . . . . . .58A Recipe: Scheduling a Runnable Task from the Main Thread . . . . . . . . . . . . . . . . . . . . . . . . .59A vi Contents Recipe: Using a Countdown Timer . . . . . . . . . . .61A Recipe: Handling a Time-Consuming Initialization . . . . . . . . . . . . . . . . . . . . . . . . . .62A Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .64A Recipe: Creating a Self-Contained Service . . . . .65A Adding a Broadcast Receiver . . . . . . . . . . . . . . . . .69A Recipe: Starting a Service When the Camera Button Is Pressed . . . . . . . . . . . . . . . . . . . . .70A App Widgets . . . . . . . . . . . . . . . . . . . . . . . . . . . . .72A Recipe: Creating an App Widget . . . . . . . . . . . .72A Alerts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .74A Recipe: Using Toast to Show a Brief Message on the Screen . . . . . . . . . . . . . . . . . . . . . . . .74A Recipe: Using an Alert Dialog Box . . . . . . . . . . .75A Recipe: Showing Notification in Status Bar . . . .76A 4 User Interface Layout . . . . . . . . . . . . . . . . . . .79A Resource Directories and General Attributes . . . . .79A Recipe: Specifying Alternate Resources . . . . . . .81A Views and ViewGroups . . . . . . . . . . . . . . . . . . . . .82A Recipe: Building Layouts in the Eclipse Editor . .83A Recipe: Controlling the Width and Height of UI Elements . . . . . . . . . . . . . . . . . . . . . . . . . .86A Recipe: Setting Relative Layout and Layout ID . .89A Recipe: Declaring a Layout Programmatically . . .90A Recipe: Updating a Layout from a Separate Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . .92A Text Manipulation . . . . . . . . . . . . . . . . . . . . . . . . .94A Recipe: Setting and Changing Text Attributes . . .95A Recipe: Providing Text Entry . . . . . . . . . . . . . . .98A Recipe: Creating a Form . . . . . . . . . . . . . . . . .100A Other Widgets: From Buttons to Seek Bars . . . . .101A Recipe: Using Image Buttons in a Table Layout . . . . . . . . . . . . . . . . . . . . . . . .102A Recipe: Using Check Boxes and Toggle Buttons . . . . . . . . . . . . . . . . . . . . . . .105A Recipe: Using Radio Buttons . . . . . . . . . . . . . .108A Recipe: Creating a Drop-Down Menu . . . . . . . .110A Recipe: Using a Progress Bar . . . . . . . . . . . . .112A Recipe: Using a SeekBar . . . . . . . . . . . . . . . .114A Contents vii 5 User Interface Events . . . . . . . . . . . . . . . . . .117A Event Handlers and Event Listeners . . . . . . . . . . .117A Recipe: Intercepting a Physical Key Press . . . .117A Recipe: Building Menus . . . . . . . . . . . . . . . . .121A Recipe: Defining Menus in XML . . . . . . . . . . . .126A Recipe: Utilizing the SEARCH Key . . . . . . . . . .127A Recipe: Reacting to Touch Events . . . . . . . . . .128A Recipe: Listening for Fling Gestures . . . . . . . .130A Recipe: Using Multitouch . . . . . . . . . . . . . . . .133A Advanced User Interface Libraries . . . . . . . . . . . .136A Recipe: Using Gestures . . . . . . . . . . . . . . . . .136A Recipe: Drawing 3D Images . . . . . . . . . . . . . .140A 6 Multimedia Techniques . . . . . . . . . . . . . . . . .147A Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .148A Recipe: Loading an Image for Manipulation . . .148A Audio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .154A Recipe: Choosing and Playing Back Audio Files . . . . . . . . . . . . . . . . . . . . . . . . .154A Recipe: Recording Audio Files . . . . . . . . . . . . .157A Recipe: Manipulating Raw Audio . . . . . . . . . . .158A Recipe: Using Sound Resources Efficiently . . .163A Recipe: Adding Media and Updating Paths . . . .165A Video . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165A 7 Hardware Interface . . . . . . . . . . . . . . . . . . . .169A Camera . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .169A Recipe: Customizing the Camera . . . . . . . . . . .170A Other Sensors . . . . . . . . . . . . . . . . . . . . . . . . . .175A Recipe: Getting a Device’s Rotational Attitude .176A Recipe: Using the Temperature and Light Sensor . . . . . . . . . . . . . . . . . . . . . . . . . . . .179A Telephony . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .180A Recipe: Utilizing the Telephony Manager . . . . .181A Recipe: Listening for Phone States . . . . . . . . .183A Recipe: Dialing a Phone Number . . . . . . . . . . .185A Bluetooth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .185A Recipe: Turning on Bluetooth . . . . . . . . . . . . .186A viii Contents Recipe: Discovering Bluetooth Devices . . . . . .187A Recipe: Pairing with Bonded Bluetooth Devices . . . . . . . . . . . . . . . . . . . . . . . . . . . .188A Recipe: Opening a Bluetooth Socket . . . . . . . .188A Recipe: Using Device Vibration . . . . . . . . . . . .191A Recipe: Accessing the Wireless Network . . . . .191A 8 Networking . . . . . . . . . . . . . . . . . . . . . . . . . .195A Using SMS . . . . . . . . . . . . . . . . . . . . . . . . . . . . .195A Recipe: Autosend an SMS Based on a Received SMS . . . . . . . . . . . . . . . . . . . . . . .197A Using Web Content . . . . . . . . . . . . . . . . . . . . . . .204A Recipe: Customizing a Web Browser . . . . . . . .204A Recipe: Using an HTTP GET . . . . . . . . . . . . . .204A Recipe: Using HTTP POST . . . . . . . . . . . . . . . .209A Social Networking . . . . . . . . . . . . . . . . . . . . . . . .210A Recipe: Integrating with Twitter . . . . . . . . . . . .210A 9 Data Storage Methods . . . . . . . . . . . . . . . . .221A Shared Preferences . . . . . . . . . . . . . . . . . . . . . .221A Recipe: Creating and Retrieving Shared Preferences . . . . . . . . . . . . . . . . . . . . . . . . .222A Recipe: Using the Preferences Framework . . . .222A Recipe: Changing the UI Based on Stored Data . . . . . . . . . . . . . . . . . . . . . . . . .225A Recipe: Adding a EULA . . . . . . . . . . . . . . . . . .228A SQLite Database . . . . . . . . . . . . . . . . . . . . . . . .232A Recipe: Creating a Separate Database Package . . . . . . . . . . . . . . . . . . . . . . . . . . .232A Recipe: Using a Separate Database Package . . . . . . . . . . . . . . . . . . . . . . . . . . .236A Recipe: Creating a Personal Diary . . . . . . . . . .239A Content Provider . . . . . . . . . . . . . . . . . . . . . . . . .243A Recipe: Creating a Custom Content Provider . .244A File Saving and Loading . . . . . . . . . . . . . . . . . . .249A 10 Location-Based Services . . . . . . . . . . . . . . . .251A Location Basics . . . . . . . . . . . . . . . . . . . . . . . . .251A Recipe: Retrieving Last Location . . . . . . . . . . .253A Contents ix Recipe: Updating Location Upon Change . . . . .254A Recipe: Listing All Enabled Providers . . . . . . . .256A Recipe: Translating a Location to Address (Reverse Geocoding) . . . . . . . . . . . . . . . . . .258A Recipe: Translating an Address to Location (Geocoding) . . . . . . . . . . . . . . . . . . . . . . . . .261A Using Google Maps . . . . . . . . . . . . . . . . . . . . . .263A Recipe: Adding Google Maps to an Application . . . . . . . . . . . . . . . . . . . . . . . . .265A Recipe: Adding Markers on a Map . . . . . . . . . .267A Recipe: Adding Views to a Map . . . . . . . . . . . .271A Recipe: Marking the Device’s Current Location on a Map . . . . . . . . . . . . . . . . . . . .274A Recipe: Setting up a Proximity Alert . . . . . . . . .274A 11 Advanced Android Development . . . . . . . . . . .277A Android Custom View . . . . . . . . . . . . . . . . . . . . .277A Recipe: Customizing a Button . . . . . . . . . . . . .277A Android Native Components . . . . . . . . . . . . . . . .283A Recipe: Developing a Native Component . . . . .284A Android Security . . . . . . . . . . . . . . . . . . . . . . . . .287A Recipe: Declaring and Enforcing Permissions . .288A Android Inter-Process Communication . . . . . . . . .288A Recipe: Implementing a Remote Procedure Call . . . . . . . . . . . . . . . . . . . . . . .289A Android Backup Manager . . . . . . . . . . . . . . . . . .294A Recipe: Creating a Backup of Runtime Data . . .294A Recipe: Backing Up Files to the Cloud . . . . . . .296A Recipe: Triggering Backup and Restore . . . . . .296A Android Animation . . . . . . . . . . . . . . . . . . . . . . .298A Recipe: Creating an Animation . . . . . . . . . . . .299A 12 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . .303A Eclipse Built-in Debug Tools . . . . . . . . . . . . . . . . .303A Recipe: Specifying a Run Configuration . . . . . .303A Recipe: Using the DDMS . . . . . . . . . . . . . . . .304A Recipe: Debugging Through Breakpoints . . . . .306A Android SDK Debug Tools . . . . . . . . . . . . . . . . . .307A Recipe: Using the Android Debug Bridge . . . . .307A

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.