www.it-ebooks.info Beginning Android Tablet Programming Starting with Android Honeycomb for Tablets (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:2) (cid:3) (cid:3) (cid:3)(cid:2) Robbie Matthews www.it-ebooks.info Beginning Android Tablet Programming: Starting with Android Honeycomb for Tablets Copyright © 2011 by Robbie Matthews 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-3783-9 ISBN-13 (electronic): 978-1-4302-3784-6 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: Steve Anglin and Tom Welsh Technical Reviewer: Stephen Bull and Peter Brownlow Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Morgan Engel, Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing, Matt Wade, Tom Welsh Coordinating Editor: Anita Castro Copy Editor: Mary Ann Fugate Compositor: Bytheway Publishing Services Indexer: SPI Global Artist: SPI Global 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 orders-ny@springer- sbm.com, 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/bulk-sales. 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. You will need to answer questions pertaining to this book in order to successfully download the code. www.it-ebooks.info I dedicate this book to my family. Without your support and occasional nagging this book would never have been finished. Robbie Matthews www.it-ebooks.info Contents at a Glance (cid:3) About the Author..................................................................................................xiv (cid:3) About the Technical Reviewer..............................................................................xv (cid:3) Acknowledgments...............................................................................................xvi (cid:3) Some Notes on Using the Downloaded Code......................................................xvii (cid:3) Chapter 1: Getting Started......................................................................................1 (cid:3) Chapter 2: How Android Works.............................................................................27 (cid:3) Chapter 3: What Can You Do with an Android Tablet?.........................................53 (cid:3) Chapter 4: Beyond Java: Programming in Python and Friends............................79 (cid:3) Chapter 5: Project 1: Media Player.....................................................................105 (cid:3) Chapter 6: Explorer.............................................................................................123 (cid:3) Chapter 7: Contact Manager...............................................................................151 (cid:3) Chapter 8: Dude, Where’s My Car?.....................................................................175 (cid:3) Chapter 10: Remind Me......................................................................................221 (cid:3) Chapter 11: Everything Else................................................................................243 (cid:3) Index...................................................................................................................269 iv www.it-ebooks.info Contents (cid:3) About the Author..................................................................................................xiv (cid:3) About the Technical Reviewer..............................................................................xv (cid:3) Acknowledgments...............................................................................................xvi (cid:3) Some Notes on Using the Downloaded Code......................................................xvii (cid:3) Chapter 1: Getting Started......................................................................................1 A Short, Personal History of Portable Programming..........................................................1 The Advent of Android.......................................................................................................3 Preparing Your Computer..................................................................................................4 Installing Your Development Environment................................................................................................4 Creating an Emulator Instance.................................................................................................................5 Setting Your Path......................................................................................................................................6 Your First Android Program...............................................................................................6 Oh, No! Java!.....................................................................................................................9 A Quick Guide to Java......................................................................................................10 Structure.................................................................................................................................................11 Primitives................................................................................................................................................12 Flow Control...........................................................................................................................................12 Objects....................................................................................................................................................13 Constructors, Initializers, and Overloading.............................................................................................17 Where’s “Free”?.....................................................................................................................................19 String Handling.......................................................................................................................................20 v www.it-ebooks.info (cid:3) CONTENTS Packages................................................................................................................................................21 Lists and Maps.......................................................................................................................................22 Generics..................................................................................................................................................23 Inheritance and Interfaces......................................................................................................................24 Annotations.............................................................................................................................................25 And Many More......................................................................................................................................26 Summary.........................................................................................................................26 (cid:3) Chapter 2: How Android Works.............................................................................27 Basic Structure of Android Programs..............................................................................27 Lifecycle.................................................................................................................................................38 Intents: What, Where, Why, and Are They Honorable?....................................................42 Intent Filters...........................................................................................................................................47 Common Intents.....................................................................................................................................48 Finally.....................................................................................................................................................48 Secretly Linux..................................................................................................................49 Summary.........................................................................................................................51 (cid:3) Chapter 3: What Can You Do with an Android Tablet?.........................................53 More Sensors Than the CIA.............................................................................................54 Lights, Camera, Action.....................................................................................................63 Browsing for Fun and Profit.............................................................................................64 Managing Your Assets............................................................................................................................66 Getting Fancy..........................................................................................................................................67 My Little Black Book—Managing Contacts.....................................................................69 Accessing Contacts................................................................................................................................69 A Quick Side Trip into Lists.....................................................................................................................72 Different Things to Access......................................................................................................................74 vi www.it-ebooks.info (cid:3) CONTENTS Share My Stuff (Sending and Receiving, Well, Everything).............................................75 Bonus Stuff—Menu Options...................................................................................................................75 Sending E-mail.......................................................................................................................................76 Sound and Fury (Managing Media Files).........................................................................77 Summary.........................................................................................................................78 (cid:3) Chapter 4: Beyond Java: Programming in Python and Friends............................79 Why Use Another Language?...........................................................................................80 Getting Started.................................................................................................................80 Script Management.........................................................................................................80 Help! I Need Somebody..........................................................................................................................82 Python Help............................................................................................................................................82 What’s a Facade?...................................................................................................................................82 Intents (Again)........................................................................................................................................83 Different Ways of Using the Intent Methods...........................................................................................84 User Interaction......................................................................................................................................86 Events.....................................................................................................................................................89 Media Files.............................................................................................................................................91 Controlling Your Phone...........................................................................................................................94 Where Am I?...........................................................................................................................................95 Battery....................................................................................................................................................97 Keeping the Device Awake.....................................................................................................................99 Editing Tips...........................................................................................................................................100 Contacts and Phone Numbers..............................................................................................................102 SQL.......................................................................................................................................................103 More Stuff.............................................................................................................................................104 Summary.......................................................................................................................104 vii www.it-ebooks.info (cid:3) CONTENTS (cid:3) Chapter 5: Project 1: Media Player.....................................................................105 Fragments......................................................................................................................105 What’s a Fragment?.............................................................................................................................105 Examining the Example........................................................................................................................106 The ActionBar.......................................................................................................................................109 FragmentManager................................................................................................................................111 Media Player Application...............................................................................................112 Displaying a List from a Cursor............................................................................................................114 Querying Media Files............................................................................................................................115 Responding to Clicks............................................................................................................................115 Some More on the ActionBar................................................................................................................115 Menu.....................................................................................................................................................116 Getting a Media URI..............................................................................................................................116 Playing Media.......................................................................................................................................117 Different Media Sources.......................................................................................................................118 Dialog Boxes.........................................................................................................................................118 Changing a List Cursor.........................................................................................................................120 Advanced Media Playing......................................................................................................................120 Displaying Our Progress.......................................................................................................................121 Controlling Our Progress......................................................................................................................122 Summary.......................................................................................................................122 (cid:3) Chapter 6: Explorer.............................................................................................123 Anatomy of a File Explorer.............................................................................................123 ListActivity for Fun and Profit...............................................................................................................124 A Note on Resources............................................................................................................................125 Build Your Own List Adapter.................................................................................................................126 Inflation.................................................................................................................................................128 Populating the List................................................................................................................................128 viii www.it-ebooks.info (cid:3) CONTENTS Finding Out About Your File..................................................................................................................130 Learning to Mime..................................................................................................................................133 Dating Your File....................................................................................................................................133 Going Native.........................................................................................................................................133 State Your Preference..........................................................................................................................137 Reading Your Preferences....................................................................................................................139 Sorting Techniques...............................................................................................................................141 The Actual Activity................................................................................................................................141 Reacting to a List Selection..................................................................................................................142 Creation at Last....................................................................................................................................143 A Different Menu...................................................................................................................................144 Reacting to a Context Menu.................................................................................................................144 Dialogs..................................................................................................................................................145 A Few Last Bits.....................................................................................................................................148 Making It Better....................................................................................................................................149 Summary.......................................................................................................................149 (cid:3) Chapter 7: Contact Manager...............................................................................151 And Now for Something Completely Different...............................................................152 The Application.....................................................................................................................................152 Anatomy of a Signpost.........................................................................................................................152 Knowing When the View Is Available....................................................................................................156 Room for Improvement.........................................................................................................................156 Backward Compatibility........................................................................................................................157 List Handling.........................................................................................................................................158 Saving the List......................................................................................................................................158 Reusing Your Libraries.........................................................................................................................159 Import and Export.................................................................................................................................159 A Last Note on Dialogs.........................................................................................................................160 ix www.it-ebooks.info
Description: