0 1 - CommonsWare: Android App Development Books

Transcription

"# %&5 ! '()* ,-)(./0180 1 1 0 234 567

Android Programming Tutorialsby Mark L. MurphySubscribe to updates at http://commonsware.comSpecial Creative Commons BY-NC-SA 3.0 License Edition

Android Programming Tutorialsby Mark L. MurphyCopyright 2009-2011 CommonsWare, LLC. All Rights Reserved.Printed in the United States of America.CommonsWare books may be purchased in printed (bulk) or digital form for educational orbusiness use. For more information, contact direct@commonsware.com.Printing History:Jan 2011: Version 3.1ISBN: 978-0-9816780-4-7The CommonsWare name and logo, “Busy Coder's Guide”, and related trade dress aretrademarks of CommonsWare, LLC.All other trademarks referenced in this book are trademarks of their respective firms.The publisher and author(s) assume no responsibility for errors or omissions or for damagesresulting from the use of the information contained herein.Subscribe to updates at http://commonsware.comSpecial Creative Commons BY-NC-SA 3.0 License Edition

Table of ContentsWelcome to the Warescription!.xviiPreface.xixWelcome to the Book!.xixPrerequisites.xixUsing the Tutorials.xxWarescription.xxiiWhat's New.xxiiAbout the "Further Reading" Sections.xxiiiErrata and Book Bug Bounty.xxiiiSource Code License.xxivCreative Commons and the Four-to-Free (42F) Guarantee.xxivLifecycle of a CommonsWare Book.xxvRoster of Tutorials.xxviYour First Android Project.1Step #1: Create the New Project.1Step #1: Eclipse.2Step #2: Command Line.5Step #2: Build, Install, and Run the Application in Your Emulator orDevice.6iiiSubscribe to updates at http://commonsware.comSpecial Creative Commons BY-NC-SA 3.0 License Edition

Step #1: Eclipse.6Step #2: Command Line.7A Simple Form.11Step-By-Step Instructions.11Step #1: Generate the Application Skeleton.11Step #2: Modify the Layout.12Step #3: Compile and Install the Application.13Step #4: Run the Application in the Emulator.14Step #5: Create a Model Class.14Step #6: Save the Form to the Model.15Extra Credit.16Further Reading.17A Fancier Form.19Step-By-Step Instructions.19Step #1: Switch to a TableLayout.19Step #2: Add a RadioGroup.21Step #3: Update the Model.23Step #4: Save the Type to the Model.24Extra Credit.26Further Reading.27Adding a List.29Step-By-Step Instructions.29Step #1: Hold a List of Restaurants.29Step #2: Save Adds to List.30Step #3: Implement toString().31Step #4: Add a ListView Widget.31Step #5: Build and Attach the Adapter.33ivSubscribe to updates at http://commonsware.comSpecial Creative Commons BY-NC-SA 3.0 License Edition

Extra Credit.35Further Reading.36Making Our List Be Fancy.37Step-By-Step Instructions.37Step #1: Create a Stub Custom Adapter.38Step #2: Design Our Row.38Step #3: Override getView(): The Simple Way.40Step #4: Create a RestaurantHolder.41Step #5: Recycle Rows via RestaurantHolder.42Extra Credit.45Further Reading.45Splitting the Tab.47Step-By-Step Instructions.47Step #1: Rework the Layout.47Step #2: Wire In the Tabs.49Step #3: Get Control On List Events.51Step #4: Update Our Restaurant Form On Clicks.51Step #5: Switch Tabs On Clicks.52Extra Credit.56Further Reading.56Menus and Messages.57Step-By-Step Instructions.57Step #1: Add Notes to the Restaurant.57Step #2: Add Notes to the Detail Form.58Step #3: Define the Option Menu.60Step #4: Show the Notes as a Toast.61Extra Credit.67vSubscribe to updates at http://commonsware.comSpecial Creative Commons BY-NC-SA 3.0 License Edition

Further Reading.67Sitting in the Background.69Step-By-Step Instructions.69Step #1: Initialize the Progress Bar.69Step #2: Create the Work Method.70Step #3: Fork the Thread from the Menu.71Step #4: Manage the Progress Bar.73Extra Credit.75Further Reading.76Life and Times.77Step-By-Step Instructions.77Step #1: Lengthen the Background Work.77Step #2: Pause in onPause().78Step #3: Resume in onResume().79Extra Credit.86Further Reading.86A Few Good Resources.87Step-By-Step Instructions.87Step #1: Review our Current Resources.87Step #2: Create a Landscape Layout.88Extra Credit.91Further Reading.92The Restaurant Store.93Step-By-Step Instructions.93Step #1: Create a Stub SQLiteOpenHelper.93Step #2: Manage our Schema.94Step #3: Remove Extraneous Code from LunchList.95viSubscribe to updates at http://commonsware.comSpecial Creative Commons BY-NC-SA 3.0 License Edition

Step #4: Get Access to the Helper.95Step #5: Save a Restaurant to the Database.96Step #6: Get the List of Restaurants from the Database.98Step #7: Change our Adapter and Wrapper.99Step #8: Clean Up Lingering ArrayList References.101Step #9: Refresh Our List.102Extra Credit.107Further Reading.107Getting More Active.109Step-By-Step Instructions.109Step #1: Create a Stub Activity.109Step #2: Launch the Stub Activity on List Click.110Step #3: Move the Detail Form UI.111Step #4: Clean Up the Original UI.114Step #5: Pass the Restaurant ID.115Step #6: Load the Restaurant Into the Form.116Step #7: Add an "Add" Menu Option.118Step #8: Detail Form Supports Add and Edit.119Extra Credit.133Further Reading.133What's Your Preference?.135Step-By-Step Instructions.135Step #1: Define the Preference XML.135Step #2: Create the Preference Activity.136Step #3: Connect the Preference Activity to the Option Menu.137Step #4: Apply the Sort Order on Startup.141Step #5: Listen for Preference Changes.142viiSubscribe to updates at http://commonsware.comSpecial Creative Commons BY-NC-SA 3.0 License Edition

Step #6: Re-Apply the Sort Order on Changes.143Extra Credit.144Further Reading.145Turn, Turn, Turn.147Step-By-Step Instructions.147Step #1: Add a Stub onSaveInstanceState().147Step #2: Pour the Form Into the Bundle.148Step #3: Repopulate the Form.148Step #4: Fix Up the Landscape Detail Form.149Extra Credit.150Further Reading.151Raising (Something Like) a Tweet.155Step-By-Step Instructions.155Step #1: Set Up an Identi.ca Account.155Step #2: Create a Stub Application and Activity.156Step #3: Create a Layout.156Step #4: Listen for Send Actions.158Step #5: Make the Status Post Request.159Extra Credit.162Further Reading.163Opening a JAR.165Step-By-Step Instructions.165Step #1: Obtain the JTwitter JAR.165Step #2: Switch from HttpClient to JTwitter.166Step #3: Create Preferences for Account Information.167Step #4: Use Account Information from Preferences.170Extra Credit.174viiiSubscribe to updates at http://commonsware.comSpecial Creative Commons BY-NC-SA 3.0 License Edition

Further Reading.175Listening To Your Friends.177Step-By-Step Instructions.177Step #1: Create a Service Stub.177Step #2: Set Up a Background Thread.178Step #3: Poll Your Friends.179Step #4: Find New Statuses.180Step #5: Set up the Public API.181Extra Credit.182Further Reading.182No, Really Listening To Your Friends.183Step-By-Step Instructions.183Step #1: Define the Callback.183Step #2: Enable Callbacks in the Service.184Step #3: Manage the Service and Register the Account.186Step #4: Display the Timeline.188Extra Credit.200Further Reading.201Your Friends Seem Remote.203Step-By-Step Instructions.203Step #1: Back Up or Branch Your Project.203Step #2: Create a Fresh Project.204Step #3: Move the Service to the New Project.204Step #4: Implement and Copy the AIDL.205Step #5: Implement the Client Side.206Step #6: Implement the Service Side.208Step #7: Restore Your Project.217ixSubscribe to updates at http://commonsware.comSpecial Creative Commons BY-NC-SA 3.0 License Edition

Extra Credit.217Further Reading.217A Subtle Notification.219Step-By-Step Instructions.219Step #1: Pick a Word and Icon.219Step #2: Raise the Notification.220Step #3: Watch for the Keyword.221Step #4: Clearing the Notification.223Extra Credit.225Further Reading.225Posts On Location.227Step-By-Step Instructions.227Step #1: Get the LocationManager.227Step #2: Register for Location Updates.228Step #3: Add "Insert Location" Menu.229Step #4: Insert the Last Known Location.230Extra Credit.232Further Reading.233Here a Post, There a Post.235Step-By-Step Instructions.235Step #1: Register for a Map API Key.235Step #2: Create a Basic MapActivity.236Step #3: Launch the Map on Location-Bearing Status Click.237Step #4: Show the Location Via a Pin.240Extra Credit.243Further Reading.243xSubscribe to updates at http://commonsware.comSpecial Creative Commons BY-NC-SA 3.0 License Edition

Media.245Step-By-Step Instructions.245Step #1: Obtain and Install a Video Clip.245Step #2: Create the Stub Helpcast Activity.246Step #3: Launch the Helpcast from the Menu.247Extra Credit.249Further Reading.250Browsing Some Posts.251Step-By-Step Instructions.251Step #1: Add Auto-Linking.251Step #2: Draft and Package the Help HTML.252Step #3: Create a Help Activity.252Step #4: Splice In the Help Activity.253Extra Credit.255Further Reading.256High-Priced Help.257Step-By-Step Instructions.257Step #1: Enable Javascript.257Step #2: Create the Java Object to Inject.258Step #3: Inject the Java Object.258Step #4: Leverage the Java Object from Javascript.259Extra Credit.260Further Reading.260Now Your Friends Seem Animated.263Step-By-Step Instructions.263Step #1: Set Up the Option Menu.263Step #2: Show and Hide the Status Entry Widgets.266xiSubscribe to updates at http://commonsware.comSpecial Creative Commons BY-NC-SA 3.0 License Edition

Step #3: Fading In and Out.268Extra Credit.270Further Reading.270Messages From The Great Beyond.271Step-By-Step Instructions.271Step #1: Broadcast the Intent.271Step #2: Catch and Use the Intent.273Extra Credit.274Further Reading.274Contacting Our Friends.275Step-By-Step Instructions.275Step #1: Fake the Contact Data.275Step #2: Design the Highlight.280Step #3: Find and Highlight Matching Contacts.281Extra Credit.282Further Reading.283Android Would Like Your Attention.285Step-By-Step Instructions.285Step #1: Track the Battery State.285Step #2: Use the Battery State.287Extra Credit.288Further Reading.288Now, Your Friends Are Alarmed.289Step-By-Step Instructions.289Step #1: Import a Reusable Component.289Step #2: Create the Alarm BroadcastReceiver.290Step #3: Doing the Work.291xiiSubscribe to updates at http://commonsware.comSpecial Creative Commons BY-NC-SA 3.0 License Edition

Extra Credit.298Further Reading.298Searching For Food.299Step-By-Step Instructions.299Step #1: Have the List Conduct the Search.299Step #2: Integrate the Search in the Application.302Extra Credit.304Further Reading.304Look Inside Yourself.305Step-By-Step Instructions.305Step #1: Create a Stub Project.305Step #2: Create a Layout.306Step #3: Find the Correct Contact Uri.307Step #4: Attach the Button to the Contacts.307Step #5: Populate the List.308Extra Credit.312Further Reading.313A Restaurant In Your Own Home.315Step-By-Step Instructions.315Step #1: Find An App Widget Background and Icon.315Step #2: Design the App Widget Layout.316Step #3: Add an (Empty) AppWidgetProvider.316Step #4: Add the Widget Metadata.317Step #5: Update the Manifest.318Step #6: Show a Random Restaurant.321Extra Credit.324Further Reading.324xiiiSubscribe to updates at http://commonsware.comSpecial Creative Commons BY-NC-SA 3.0 License Edition

More Home Cooking.325Step-By-Step Instructions.325Step #1: Find a Button Graphic.325Step #2: Add the Button to the Layout.326Step #3: Migrate Update Logic to an IntentService.

Android Programming Tutorials by Mark L. Murphy Subscribe to updates at http://commonsware.