www.it-ebooks.info
www.it-ebooks.info
Android
™
Application Development
Cookbook
93 RECIPES FOR BUILDING WINNING APPS
Wei-Meng Lee
www.it-ebooks.info
Android™ Application Development Cookbook: 93 Recipes for Building Winning Apps
Published by
John Wiley & Sons, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2013 by John Wiley & Sons, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-1-118-17767-9
ISBN: 978-1-118-22729-9 (ebk)
ISBN: 978-1-118-24028-1 (ebk)
ISBN: 978-1-118-26491-1 (ebk)
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108
of the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization
through payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers,
MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the
Permissions Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201)
748-6008, or online at http://www.wiley.com/go/permissions.
Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or warranties with
respect to the accuracy or completeness of the contents of this work and specically disclaim all warranties, including
without limitation warranties of tness for a particular purpose. No warranty may be created or extended by sales or pro-
motional materials. The advice and strategies contained herein may not be suitable for every situation. This work is sold
with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional services.
If professional assistance is required, the services of a competent professional person should be sought. Neither the pub-
lisher nor the author shall be liable for damages arising herefrom. The fact that an organization or Web site is referred to
in this work as a citation and/or a potential source of further information does not mean that the author or the publisher
endorses the information the organization or Web site may provide or recommendations it may make. Further, readers
should be aware that Internet Web sites listed in this work may have changed or disappeared between when this work was
written and when it is read.
For general information on our other products and services please contact our Customer Care Department within the
United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Wiley publishes in a variety of print and electronic formats and by print-on-demand. Some material included with
standard print versions of this book may not be included in e-books or in print-on-demand. If this book refers to media
such as a CD or DVD that is not included in the version you purchased, you may download this material at http://
booksupport.wiley.com
. For more information about Wiley products, visit www.wiley.com.
Library of Congress Control Number: 2012948549
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related trade dress are
trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its afliates, in the United States and other coun-
tries, and may not be used without written permission. Android is a trademark of Google, Inc. All other trademarks are
the property of their respective owners. John Wiley & Sons, Inc., is not associated with any product or vendor mentioned
in this book.
www.it-ebooks.info
To my family,
Thanks for the understanding and support while I
worked on getting this book ready. I love you all!
www.it-ebooks.info
www.it-ebooks.info
ABOUT THE AUTHOR
WEIMENG LEE is a technologist and founder of Developer Learning Solutions (www.learn2develop
.net
), a technology company specializing in hands-on training on the latest mobile technologies.
Wei-Meng has many years of training experience and his courses place special emphasis on the learn-
ing-by-doing approach. This hands-on approach to learning programming makes understanding the
subject much easier than reading books, tutorials, and other documentation.
Wei-Meng is also the author of Beginning iOS 5 Application Development (Wrox, 2010)
and Beginning Android 4 Application Development (Wrox, 2011). You can contact him at
weimenglee@learn2develop.net.
ABOUT THE TECHNICAL EDITOR
CHAIM KRAUSE is a Simulation Specialist at the US Army’s Command and General Staff College
where he develops various software products on a multitude of platforms, from iOS and Android
devices to Windowsdesktops and Linux servers, among other duties. Python is his preferred language,
but he is multilingual and also codes in Java, JavaScript/HTML5/CSS, and others. He was fortunate to
begin his professional career in the software eld at Borland where he was a Senior Developer Support
Engineer for Delphi. Outside of computer geek stuff, Chaim enjoys techno and dubstep music, and
scootering with his two sled dogs Dasher and Minnie.
www.it-ebooks.info
EXECUTIVE EDITOR
Robert Elliott
SENIOR PROJECT EDITOR
Ami Frank Sullivan
TECHNICAL EDITOR
Chaim Krause
PRODUCTION EDITOR
Christine Mugnolo
COPY EDITOR
Luann Rou
EDITORIAL MANAGER
Mary Beth Wakefield
FREELANCER EDITORIAL MANAGER
Rosemarie Graham
ASSOCIATE DIRECTOR OF MARKETING
David Mayhew
MARKETING MANAGER
Ashley Zurcher
BUSINESS MANAGER
Amy Knies
PRODUCTION MANAGER
Tim Tate
VICE PRESIDENT AND EXECUTIVE GROUP
PUBLISHER
Richard Swadley
VICE PRESIDENT AND EXECUTIVE PUBLISHER
Neil Edde
ASSOCIATE PUBLISHER
Jim Minatel
PROJECT COORDINATOR, COVER
Katie Crocker
COMPOSITOR
Craig Johnson, Happenstance Type-O-Rama
PROOFREADER
Scott Klemp, Word One New York
INDEXER
Robert Swanson
COVER DESIGNER
Ryan Sneed
COVER IMAGE
© Paul Fleet / iStockPhoto
CREDITS
www.it-ebooks.info
ACKNOWLEDGMENTS
A LOT OF DEVELOPMENT in the Android world has happened since my last book, Beginning Android 4
Application Development, went to print. Google has released a new version of the SDK: Android4.1
SDK. With the Android 4.1 SDK and the ADT Plugin 20.0.3, it is now much easier to write Android
applications — from those that run on older devices right up to the latest and greatest.
I would like to thank some key people who have worked hard behind the scenes to make this book
a reality.
First, my personal gratitude to Bob Elliott, executive editor at Wrox. Bob is always ready to lend a
listening ear and offer help when it’s needed. It is a great pleasure to work with Bob, as he is one of
the most responsive people I have ever worked with. Thank you, Bob, for the help and guidance.
Of course, I cannot forget Ami Sullivan, my editor (and friend), who is always a pleasure to work
with. Thank you for your guidance and encouragement to keep the project going, Ami.
I am also grateful to my technical editor, Chaim Krause. Chaim has been eagle-eye editing the book
and testing my recipes, ensuring that my code works as written. Thanks, Chaim.
Last but not least, I want to thank my parents and my wife, Sze Wa, for all the support they have
given me. They have selessly adjusted their schedules to accommodate my busy schedule when I
was working on this book. My wife, as always, has stayed up with me on numerous nights as I was
furiously working to meet the deadlines, and for this I would like to say to her and my parents, “I
love you all!” Finally, to our lovely dog, Ookii, thanks for staying by our side.
www.it-ebooks.info
www.it-ebooks.info
[...]... Activity2: < /application> ... a Hello World application, chances are good that your application contains several activities that you need to connect in order to form a cohesive application This recipe shows you the various ways to link to another activity in your Android application www.it-ebooks.info 2 ❘ CHAPTER 1 Android Fundamentals Solution Suppose you have two activities in your application The following AndroidManifest.xml... www.it-ebooks.info 1 Android Fundamentals In this chapter, you learn about the fundamental topics in Android that most developers need to know, including how to link to other applications using the Intent object, how to communicate with other applications (or parts of the same application) using broadcast receivers, and how to pass data between activities RECIPE 1.1 LINKING ACTIVITIES Android Versions... create an Android project using Eclipse, and that you are already familiar with the structure of an Android project All the code samples in this book were written and tested using the Android 4.1 SDK, together with Eclipse (Juno release) and the ADT plugin 20.0.3 All projects are able to run on Android devices beginning with Android version 2.2 In particular, all projects make use of the Android Support... element within the element: android: name="net.learn2develop.Activity2" /> > If the activity you are trying to call does not exist on the device, your application will crash, displaying a message like the... activity, use the setResult() method, as shown in the following code: package net.learn2develop.passingdata; import import import import import import android. app.Activity; android. content.Intent; android. net.Uri; android. os.Bundle; android. view.View; android. widget.Toast; public class SecondActivity extends Activity { www.it-ebooks.info Recipe 1.2 Passing Data Between Activities @Override public... Deploying Your Android Applications Recipe 11.1 Localizing Your Application 357 357 Solution 358 Recipe 11.2 Exporting Your Application as an APK File 362 Solution 362 Recipe 11.3 Deploying Your App Through E‑mail 365 Solution 365 Recipe 11.4 Deploying Your App Through the Web 367 Solution 368 Recipe 11.5 Deploying Your App Through an SD Card 369 Solution 369 Recipe 11.6 Specifying the Application . www.it-ebooks.info
www.it-ebooks.info
Android
™
Application Development
Cookbook
93 RECIPES FOR BUILDING WINNING APPS
Wei-Meng Lee
www.it-ebooks.info
Android Application Development Cookbook: . and other documentation.
Wei-Meng is also the author of Beginning iOS 5 Application Development (Wrox, 2010)
and Beginning Android 4 Application Development
Ngày đăng: 23/03/2014, 02:20
Xem thêm: Android Application Development Cookbook doc, Android Application Development Cookbook doc, Recipe 1.2 Passing Data Between Activities, Recipe 1.4 Sending and Receiving Broadcasts, Recipe 1.5 Assigning Priorities to Broadcast Receivers, Recipe 1.6 Auto-Launching Your Application at Boot Time, Recipe 1.8 Making Your Application Callable By Others, Recipe 2.5 Implementing a Star Rating System, Recipe 2.10 Using LinearLayout for View Positioning, Recipe 2.12 Using FrameLayout for View Positioning, Recipe 2.13 Using TableLayout for View Positioning, Recipe 2.15 Displaying Context and Options Menus, Recipe 3.1 Displaying a List of Items Using the ListView, Recipe 3.5 Further Customizing Each Row with Additional TextViews, Recipe 3.6 Displaying a List of Items Using the Spinner View, Recipe 3.7 Displaying a List of Images, Recipe 3.8 Animating the Changing of Images Using the ImageSwitcher, Recipe 3.9: Displaying Images Using the GridView, Recipe 3.10 Building a Master-Detail User Interface, Recipe 4.2 Monitoring the State of the Phone, Recipe 4.3 Monitoring Phone State in the Background, Recipe 4.5 Auto-Answering an Incoming Call, Recipe 4.7 Getting the Phone Number, IMEI, and SIM Card ID, Recipe 5.1 Sending SMS Messages Through the Built-in Messaging Application, Recipe 5.3 Monitoring the Status of Sent SMS Messages Programmatically, Recipe 5.4 Monitoring Outgoing SMS Messages, Recipe 5.5 Intercepting Incoming SMS Messages, Recipe 6.1 Connecting to Servers Using HTTP GET, Recipe 6.2 Connecting to Servers Using HTTP POST, Recipe 6.4 Consuming XML Web Services, Recipe 6.5 Consuming JSON Web Services, Recipe 6.7 Creating a Socket Server, Recipe 6.8 Creating a Socket Client, Recipe 6.11 Creating a Bluetooth Chat Application, Recipe 7.2 Zooming in and out of Google Maps, Recipe 7.5 Adding Markers to the Map, Recipe 7.6 Finding a User-Friendly Address Using Reverse Geocoding, and Vice Versa, Recipe 7.7 Responding to Zooming and Panning, Recipe 8.1 Obtaining Geographical Location Using GPS, Wi-Fi, or Cellular Networks, Recipe 8.2 Choosing the Best Location Provider to Use, Recipe 8.4 Using a BroadcastReceiver to Obtain Locations, Recipe 9.1 Capturing Pictures with the Camera, Recipe 9.2 Detecting the Presence of Hardware Features, Recipe 9.5 Capturing Hardware Buttons Programmatically, Recipe 9.6 Switching on the Flashlight, Recipe 10.1 Saving and Loading User Preferences, Recipe 10.2 Creating a Preference Screen, Recipe 10.3 Saving Files to the Data Directory, Recipe 10.5 Saving Files to External Storage, Recipe 10.7 Creating and Using SQLite Databases Programmatically, Recipe 10.8 Pre-creating the SQLite Databases, Recipe 11.2 Exporting Your Application as an APK File, Recipe 11.6 Specifying the Application Installation Location