Docsity

Android Programming Tutorials 2.9, Notas de estudo de Introdução à Programação de Computadores

Pré-visualização parcial do texto

Baixe Android Programming Tutorials 2.9 e outras Notas de estudo em PDF para Introdução à Programação de Computadores, somente na Docsity!

This document is licensed for Keith Rasmussen's exclusive use by CommonsWare, LLC COMMONSWARE

Programming

Tutorials

Mark L. Murphy Version 2.

for Android

Android™

Android Programming Tutorials

by Mark L. Murphy

Table of Contents Welcome to the Warescription!.........................................................xvii Preface..................................................................................................xix Welcome to the Book!...............................................................................xix Prerequisites...............................................................................................xix Using the Tutorials.....................................................................................xx Warescription............................................................................................xxi What's New...............................................................................................xxii About the "Further Reading" Sections....................................................xxii Errata and Book Bug Bounty..................................................................xxiii Source Code License................................................................................xxiv Creative Commons and the Four-to-Free (42F) Guarantee.................xxiv Lifecycle of a CommonsWare Book........................................................xxv Roster of Tutorials...................................................................................xxvi Your First Android App........................................................................... 1 Step-By-Step Instructions............................................................................. 1 Step #1: Choose a Place For Your Applications.................................... 1 Step #2: Check Your Java Environment................................................ 2 Step #3: Download and Install the Android SDK................................ 3 Step #4: Generate the Application Files............................................... 5 iii

iv

 - Step #5: Examine and Modify the Layout File..................................... - Step #6: Examine the Activity Java Source.......................................... - Step #7: Install Ant................................................................................. - Step #8: Compile the Application......................................................... - Step #9: Configure and Start Your Emulator....................................... - Step #10: Install the Application in Your Emulator........................... - Step #11: Run the Application in Your Emulator................................. 
  • Extra Credit..................................................................................................
  • Further Reading...........................................................................................
  • A Simple Form........................................................................................
    • Step-By-Step Instructions...........................................................................
      • Step #1: Generate the Application Skeleton........................................
      • Step #2: Modify the Layout..................................................................
      • Step #3: Compile and Install the Application.....................................
      • Step #4: Run the Application in the Emulator...................................
      • Step #5: Create a Model Class..............................................................
      • Step #6: Save the Form to the Model..................................................
    • Extra Credit.................................................................................................
    • Further Reading...........................................................................................
  • A Fancier Form.......................................................................................
    • Step-By-Step Instructions..........................................................................
      • Step #1: Switch to a TableLayout.........................................................
      • Step #2: Add a RadioGroup.................................................................
      • Step #3: Update the Model..................................................................
      • Step #4: Save the Type to the Model..................................................
    • Extra Credit.................................................................................................
    • Further Reading...........................................................................................
  • Adding a List..........................................................................................
    • Step-By-Step Instructions..........................................................................
      • Step #1: Hold a List of Restaurants......................................................
      • Step #2: Save Adds to List....................................................................
      • Step #3: Implement toString().............................................................
      • Step #4: Add a ListView Widget..........................................................
      • Step #5: Build and Attach the Adapter...............................................
    • Extra Credit.................................................................................................
    • Further Reading..........................................................................................
  • Making Our List Be Fancy.....................................................................
    • Step-By-Step Instructions...........................................................................
      • Step #1: Create a Stub Custom Adapter..............................................
      • Step #2: Design Our Row.....................................................................
      • Step #3: Override getView(): The Simple Way..................................
      • Step #4: Create a RestaurantHolder...................................................
      • Step #5: Recycle Rows via RestaurantHolder....................................
    • Extra Credit.................................................................................................
    • Further Reading..........................................................................................
  • Splitting the Tab.....................................................................................
    • Step-By-Step Instructions...........................................................................
      • Step #1: Rework the Layout..................................................................
      • Step #2: Wire In the Tabs....................................................................
      • Step #3: Get Control On List Events...................................................
      • Step #4: Update Our Restaurant Form On Clicks.............................
      • Step #5: Switch Tabs On Clicks...........................................................
    • Extra Credit.................................................................................................
    • Further Reading..........................................................................................
  • Menus and Messages.............................................................................
    • Step-By-Step Instructions...........................................................................
      • Step #1: Add Notes to the Restaurant.................................................
      • Step #2: Add Notes to the Detail Form..............................................
      • Step #3: Define the Option Menu.......................................................
      • Step #4: Show the Notes as a Toast....................................................
    • Extra Credit..................................................................................................
    • Further Reading...........................................................................................
  • Sitting in the Background.....................................................................
    • Step-By-Step Instructions..........................................................................
      • Step #1: Initialize the Progress Bar......................................................
      • Step #2: Create the Work Method......................................................
      • Step #3: Fork the Thread from the Menu...........................................
      • Step #4: Manage the Progress Bar.......................................................
    • Extra Credit.................................................................................................
    • Further Reading..........................................................................................
  • Life and Times........................................................................................
    • Step-By-Step Instructions...........................................................................
      • Step #1: Lengthen the Background Work...........................................
      • Step #2: Pause in onPause()................................................................
      • Step #3: Resume in onResume().........................................................
    • Extra Credit.................................................................................................
    • Further Reading..........................................................................................
  • A Few Good Resources...........................................................................
    • Step-By-Step Instructions...........................................................................
      • Step #1: Review our Current Resources...............................................
      • Step #2: Create a Landscape Layout...................................................
    • Extra Credit.................................................................................................
    • Further Reading..........................................................................................
  • The Restaurant Store............................................................................
    • Step-By-Step Instructions..........................................................................
      • Step #1: Create a Stub SQLiteOpenHelper.........................................
      • Step #2: Manage our Schema..............................................................
      • Step #3: Remove Extraneous Code from LunchList..........................
      • Step #4: Get Access to the Helper......................................................
      • Step #5: Save a Restaurant to the Database......................................
      • Step #6: Get the List of Restaurants from the Database..................
      • Step #7: Change our Adapter and Wrapper......................................
      • Step #8: Clean Up Lingering ArrayList References..........................
      • Step #9: Refresh Our List...................................................................
    • Extra Credit.................................................................................................
    • Further Reading..........................................................................................
  • Getting More Active..............................................................................
    • Step-By-Step Instructions..........................................................................
      • Step #1: Create a Stub Activity............................................................
      • Step #2: Launch the Stub Activity on List Click................................
      • Step #3: Move the Detail Form UI......................................................
      • Step #4: Clean Up the Original UI.....................................................
      • Step #5: Pass the Restaurant _ID........................................................
      • Step #6: Load the Restaurant Into the Form....................................
      • Step #7: Add an "Add" Menu Option................................................
      • Step #8: Detail Form Supports Add and Edit...................................
    • Extra Credit................................................................................................
    • Further Reading.........................................................................................
  • What's Your Preference?......................................................................
    • Step-By-Step Instructions.........................................................................
      • Step #1: Define the Preference XML..................................................
      • Step #2: Create the Preference Activity.............................................
      • Step #3: Connect the Preference Activity to the Option Menu......
      • Step #4: Apply the Sort Order on Startup.........................................
      • Step #5: Listen for Preference Changes............................................
      • Step #6: Re-Apply the Sort Order on Changes.................................
    • Extra Credit................................................................................................
    • Further Reading.........................................................................................
  • Turn, Turn, Turn..................................................................................
    • Step-By-Step Instructions.........................................................................
      • Step #1: Add a Stub onSaveInstanceState()......................................
      • Step #2: Pour the Form Into the Bundle...........................................
      • Step #3: Repopulate the Form............................................................
      • Step #4: Fix Up the Landscape Detail Form.....................................
    • Extra Credit................................................................................................
    • Further Reading.........................................................................................
  • Raising (Something Like) a Tweet.......................................................
    • Step-By-Step Instructions.........................................................................
      • Step #1: Set Up an Identi.ca Account.................................................
      • Step #2: Create a Stub Application and Activity..............................
      • Step #3: Create a Layout.....................................................................
      • Step #4: Listen for Send Actions.......................................................
      • Step #5: Make the Status Post Request..............................................
    • Extra Credit................................................................................................
    • Further Reading.........................................................................................
  • Opening a JAR......................................................................................
    • Step-By-Step Instructions.........................................................................
      • Step #1: Obtain the JTwitter JAR.......................................................
      • Step #2: Switch from HttpClient to JTwitter....................................
      • Step #3: Create Preferences for Account Information.....................
      • Step #4: Use Account Information from Preferences......................
    • Extra Credit................................................................................................
    • Further Reading.........................................................................................
  • Listening To Your Friends...................................................................
    • Step-By-Step Instructions.........................................................................
      • Step #1: Create a Service Stub............................................................
      • Step #2: Set Up a Background Thread..............................................
      • Step #3: Poll Your Friends...................................................................
      • Step #4: Find New Statuses................................................................
      • Step #5: Set up the Public API...........................................................
    • Extra Credit................................................................................................
    • Further Reading........................................................................................
  • No, Really Listening To Your Friends.................................................
    • Step-By-Step Instructions.........................................................................
      • Step #1: Define the Callback...............................................................
      • Step #2: Enable Callbacks in the Service..........................................
      • Step #3: Manage the Service and Register the Account..................
      • Step #4: Display the Timeline............................................................
    • Extra Credit...............................................................................................
    • Further Reading........................................................................................
  • Your Friends Seem Remote.................................................................
    • Step-By-Step Instructions........................................................................
      • Step #1: Back Up or Branch Your Project.........................................
      • Step #2: Create a Fresh Project.........................................................
      • Step #3: Move the Service to the New Project.................................
      • Step #4: Implement and Copy the AIDL..........................................
      • Step #5: Implement the Client Side..................................................
      • Step #6: Implement the Service Side................................................
      • Step #7: Restore Your Project.............................................................
    • Extra Credit................................................................................................
    • Further Reading.........................................................................................
  • A Subtle Notification............................................................................
    • Step-By-Step Instructions.........................................................................
      • Step #1: Pick a Word and Icon...........................................................
      • Step #2: Raise the Notification..........................................................
      • Step #3: Watch for the Keyword........................................................
      • Step #4: Clearing the Notification.....................................................
    • Extra Credit...............................................................................................
    • Further Reading........................................................................................
  • Posts On Location................................................................................
    • Step-By-Step Instructions........................................................................
      • Step #1: Get the LocationManager....................................................
      • Step #2: Register for Location Updates............................................
      • Step #3: Add "Insert Location" Menu...............................................
      • Step #4: Insert the Last Known Location.........................................
    • Extra Credit...............................................................................................
    • Further Reading........................................................................................
  • Here a Post, There a Post.....................................................................
    • Step-By-Step Instructions........................................................................
      • Step #1: Register for a Map API Key..................................................
      • Step #2: Create a Basic MapActivity.................................................
      • Step #3: Launch the Map on Location-Bearing Status Click...........
      • Step #4: Show the Location Via a Pin...............................................
    • Extra Credit...............................................................................................
    • Further Reading........................................................................................
  • Media....................................................................................................
    • Step-By-Step Instructions........................................................................
      • Step #1: Obtain and Install a Video Clip...........................................
      • Step #2: Create the Stub Helpcast Activity......................................
      • Step #3: Launch the Helpcast from the Menu.................................
    • Extra Credit...............................................................................................
    • Further Reading........................................................................................
  • Browsing Some Posts...........................................................................
    • Step-By-Step Instructions.........................................................................
      • Step #1: Add Auto-Linking.................................................................
      • Step #2: Draft and Package the Help HTML....................................
      • Step #3: Create a Help Activity..........................................................
      • Step #4: Splice In the Help Activity..................................................
    • Extra Credit...............................................................................................
    • Further Reading........................................................................................
  • High-Priced Help.................................................................................
    • Step-By-Step Instructions........................................................................
      • Step #1: Enable Javascript...................................................................
      • Step #2: Create the Java Object to Inject..........................................
      • Step #3: Inject the Java Object..........................................................
      • Step #4: Leverage the Java Object from Javascript..........................
    • Extra Credit...............................................................................................
    • Further Reading.........................................................................................
  • Now Your Friends Seem Animated.....................................................
    • Step-By-Step Instructions........................................................................
      • Step #1: Set Up the Option Menu.....................................................
      • Step #2: Show and Hide the Status Entry Widgets.........................
      • Step #3: Fading In and Out................................................................
    • Extra Credit...............................................................................................
    • Further Reading........................................................................................
  • Messages From The Great Beyond......................................................
    • Step-By-Step Instructions........................................................................
      • Step #1: Broadcast the Intent.............................................................
      • Step #2: Catch and Use the Intent....................................................
    • Extra Credit...............................................................................................
    • Further Reading........................................................................................
  • Contacting Our Friends.......................................................................
    • Step-By-Step Instructions........................................................................
      • Step #1: Fake the Contact Data..........................................................
      • Step #2: Design the Highlight...........................................................
      • Step #3: Find and Highlight Matching Contacts.............................
    • Extra Credit...............................................................................................
    • Further Reading........................................................................................
  • Android Would Like Your Attention..................................................
    • Step-By-Step Instructions........................................................................
      • Step #1: Track the Battery State........................................................
      • Step #2: Use the Battery State...........................................................
    • Extra Credit...............................................................................................
    • Further Reading........................................................................................
  • Now, Your Friends Are Alarmed..........................................................
    • Step-By-Step Instructions.........................................................................
      • Step #1: Import a Reusable Component............................................
      • Step #2: Create the Alarm BroadcastReceiver.................................
      • Step #3: Doing the Work...................................................................
    • Extra Credit...............................................................................................
    • Further Reading........................................................................................
  • Searching For Food..............................................................................
    • Step-By-Step Instructions.........................................................................
      • Step #1: Have the List Conduct the Search.......................................
      • Step #2: Integrate the Search in the Application.............................
    • Extra Credit...............................................................................................
    • Further Reading........................................................................................
  • Look Inside Yourself............................................................................
    • Step-By-Step Instructions........................................................................
      • Step #1: Create a Stub Project............................................................
      • Step #2: Create a Layout....................................................................
      • Step #3: Find the Correct Contact Uri..............................................
      • Step #4: Attach the Button to the Contacts.....................................
      • Step #5: Populate the List...................................................................
    • Extra Credit................................................................................................
    • Further Reading.........................................................................................
  • A Restaurant In Your Own Home........................................................
    • Step-By-Step Instructions.........................................................................
      • Step #1: Find An App Widget Background and Icon........................
      • Step #2: Design the App Widget Layout...........................................
      • Step #3: Add an (Empty) AppWidgetProvider.................................
      • Step #4: Add the Widget Metadata...................................................
      • Step #5: Update the Manifest............................................................
      • Step #6: Show a Random Restaurant................................................
    • Extra Credit...............................................................................................
    • Further Reading........................................................................................
  • More Home Cooking............................................................................
    • Step-By-Step Instructions........................................................................
      • Step #1: Find a Button Graphic..........................................................
      • Step #2: Add the Button to the Layout.............................................
      • Step #3: Migrate Update Logic to an IntentService.........................
      • Step #4: Get Control on Button Clicks..............................................
      • Step #5: Get Control on Name Clicks...............................................
    • Extra Credit................................................................................................
    • Further Reading........................................................................................
  • Take a Monkey to Lunch......................................................................
    • Step-By-Step Instructions.........................................................................
      • Step #1: Prep LunchList......................................................................
      • Step #2: Run the Monkey...................................................................
    • Extra Credit...............................................................................................
    • Further Reading........................................................................................
  • Asking Permission to Place a Call.......................................................
    • Step-By-Step Instructions.........................................................................
      • Step #1: Add a Phone Number to the Database Schema.................
      • Step #2: Intelligently Handle Database Updates.............................
      • Step #3: Add Phone Number Support to the Rest of the Helper.
      • Step #4: Collect the Phone Number on the Detail Form................
      • Step #5: Ask for Permission to Make Calls.......................................
      • Step #6: Dial the Number..................................................................
      • Step #7: Make the Call.......................................................................
    • Extra Credit...............................................................................................
    • Further Reading........................................................................................
  • Photographic Memory.........................................................................
    • Step-By-Step Instructions.........................................................................
      • Step #1: Adjust the Manifest...............................................................
      • Step #2: Create the Photographer Layout........................................
      • Step #3: Create the Photographer Class...........................................
      • Step #4: Tie In the Photographer Class............................................
    • Extra Credit...............................................................................................
    • Further Reading........................................................................................
  • Sensing a Disturbance.........................................................................
    • Step-By-Step Instructions........................................................................
      • Step #1: Implement a Shaker.............................................................
      • Step #2: Hook Into the Shaker..........................................................
      • Step #3: Make a Random Selection on a Shake...............................
    • Extra Credit...............................................................................................
    • Further Reading........................................................................................
  • Getting the Word Out..........................................................................
    • Step-By-Step Instructions........................................................................
      • Step #1: Add a "Send SMS" Option Menu........................................
      • Step #2: Find Contacts' Mobile Numbers.........................................
      • Step #3: Pick a Person........................................................................
      • Step #4: Send the Message................................................................
    • Extra Credit................................................................................................
    • Further Reading........................................................................................
  • Seeking the Proper Level.....................................................................
    • Step-By-Step Instructions.........................................................................
      • Step #1: Define an Interface for Mobile Numbers............................
      • Step #2: Implement the Interface: the New Way............................
      • Step #3: Implement the Interface: the Old Way..............................
      • Step #4: Choose and Use the Bridge.................................................
    • Extra Credit...............................................................................................
    • Further Reading........................................................................................
  • Source code listings are incorporated as graphics so as to retain the monospace font, though this means the source code listings do not honor changes in Kindle font size xviii

Preface Welcome to the Book! If you come to this book after having read its companion volumes, The Busy Coder's Guide to Android Development and The Busy Coder's Guide to Advanced Android Development , thanks for sticking with the series! CommonsWare aims to have the most comprehensive set of Android development resources (outside of the Open Handset Alliance itself), and we appreciate your interest. If you come to this book having learned about Android from other sources, thanks for joining the CommonsWare community! Prerequisites This book is a collection of tutorials, walking you through developing Android applications, from the simplest "Hello, world!" to applications using many advanced Android APIs. Since this book only supplies tutorials, you will want something beyond it as a reference guide. That could be simply the Android SDK documentation, available with your SDK installation or online. It could be the other books in the CommonsWare Android series. Or, it could be another Android book – a list of currently-available Android books can be found on the Android Programming knol. What you do not want to do is xix