apress beginning android 3 (2011)

573 673 0
  • Loading ...
1/573 trang
Tải xuống

Thông tin tài liệu

Ngày đăng: 31/03/2014, 16:34

Beginning Android 3 ■ ■ ■ Mark Murphy Intel’s Recommended Reading ListSelected ForBeginning Android 3 Copyright © 2011 by Mark Murphy 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-3297-1 ISBN-13 (electronic): 978-1-4302-3298-8 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 Development Editor: Tom Welsh Technical Reviewer: Dylan Philips Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, James Markham, Jeff Olson, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Jessica Belanger Copy Editor: William McManus Compositor: MacPS, LLC Indexer: John Collin Artist: April Milne 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 rights@apress.com, 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. Contents Part I: Core Concept 1■Chapter 1: The Big Picture 3Benefits and Drawbacks of Smartphone Programming 3What Androids Are Made Of 4Stuff at Your Disposal 5The Big Picture of This Book 6■Chapter 2: How to Get Started 7Step 1: Set Up Java 7Install the JDK 7Learn Java 8Step 2: Install the Android SDK 8Install the Base Tools 8Install the SDKs and Add-ons 9Step 3: Install the ADT for Eclipse 12Step 4: Install Apache Ant 14Step 5: Set Up the Emulator 15Step 6: Set Up the Device 21Windows 21Mac OS X and Linux 22■Chapter 3: Your First Android Project 23Step 1: Create the New Project 23Eclipse 23Command Line 26Step 2: Build, Install, and Run the Application in Your Emulator or Device 27Eclipse 27Command Line 28■Chapter 4: Examining Your First Project 31Project Structure 31Root Contents 31The Sweat Off Your Brow 32And Now, the Rest of the Story 32What You Get Out of It 33Inside Your Manifest 33In the Beginning, There Was the Root, and It Was Good 34An Application for Your Application 35■Chapter 5: A Bit About Eclipse 37What the ADT Gives You 37Coping with Eclipse 38How to Import a Non-Eclipse Project 38How to Get to DDMS 42How to Create an Emulator 43How to Run a Project 44How Not to Run Your Project 45Alternative IDEs 45IDEs and This Book 46■Chapter 6: Enhancing Your First Project 47Supporting Multiple Screen Sizes 47Specifying Versions 48Part II: Activities 49■Chapter 7: Rewriting Your First Project 51The Activity 51Dissecting the Activity 52Building and Running the Activity 53■Chapter 8: Using XML-Based Layouts 55What Is an XML-Based Layout? 55Why Use XML-Based Layouts? 55OK, So What Does It Look Like? 56What’s with the @ Signs? 57And How Do We Attach These to the Java? 57The Rest of the Story 58■Chapter 9: Employing Basic Widgets 61Assigning Labels 61Button, Button, Who’s Got the Button? 62Fleeting Images 63Fields of Green…or Other Colors 64Just Another Box to Check 66Turn the Radio Up 68It’s Quite a View 70Padding 70Other Useful Properties 71Useful Methods 71Colors 71■Chapter 10: Working with Containers 73Thinking Linearly 73LinearLayout Concepts and Properties 74LinearLayout Example 76The Box Model 80All Things Are Relative 81RelativeLayout Concepts and Properties 82RelativeLayout Example 84Overlap 86Tabula Rasa 87TableLayout Concepts and Properties 87TableLayout Example 89Scrollwork 90■Chapter 11: The Input Method Framework 93Keyboards, Hard and Soft 93Tailored to Your Needs 94Tell Android Where It Can Go 98Fitting In 100Jane, Stop This Crazy Thing! 101■Chapter 12: Using Selection Widgets 103Adapting to the Circumstances 103Using ArrayAdapter 104Lists of Naughty and Nice 104Selection Modes 106Spin Control 108Grid Your Lions (or Something Like That ) 111Fields: Now with 35% Less Typing! 115Galleries, Give or Take the Art 118■Chapter 13: Getting Fancy with Lists 119Getting to First Base 119A Dynamic Presentation 121Inflating Rows Ourselves 123A Sidebar About Inflation 123And Now, Back to Our Story 125Better. Stronger. Faster. 125Using convertView 125Using the Holder Pattern 127Interactive Rows 129■Chapter 14: Still More Widgets and Containers 135Pick and Choose 135Time Keeps Flowing Like a River 140Seeking Resolution 141Putting It on My Tab 142The Pieces 143Wiring It Together 144Adding Them Up 146 Flipping Them Off 149Getting in Somebody’s Drawer 154Other Good Stuff 156■Chapter 15: Embedding the WebKit Browser 159A Browser, Writ Small 159Loading It Up 161Navigating the Waters 162Entertaining the Client 163Settings, Preferences, and Options (Oh, My!) 165■Chapter 16: Applying Menus 167Flavors of Menu 167Menus of Options 168Menus in Context 169Taking a Peek 170Yet More Inflation 175Menu XML Structure 175Menu Options and XML 176Inflating the Menu 177In the Land of Menus and Honey 178■Chapter 17: Showing Pop-Up Messages 179Raising Toasts 179Alert! Alert! 180Checking Them Out 181■Chapter 18: Handling Activity Lifecycle Events 183Schroedinger’s Activity 183Life, Death, and Your Activity 184onCreate() and onDestroy() 184onStart(), onRestart(), and onStop() 185onPause() and onResume() 185The Grace of State 185■Chapter 19: Handling Rotation 187A Philosophy of Destruction 187It’s All the Same, Just Different 188Picking and Viewing a Contact 189Saving Your State 190Now with More Savings! 193DIY Rotation 195 But Google Does Not Recommend This 198Forcing the Issue 198Making Sense of It All 200■Chapter 20: Dealing with Threads 203The Main Application Thread 203Making Progress with ProgressBars 204Getting Through the Handlers 204Messages 205Runnables 208 Where Oh Where Has My UI Thread Gone? 208Asyncing Feeling 208The Theory 208AsyncTask, Generics, and Varargs 209The Stages of AsyncTask 209A Sample Task 210Threads and Rotation 214Manual Activity Association 215Flow of Events 217Why This Works 218And Now, the Caveats 218■Chapter 21: Creating Intent Filters 221What’s Your Intent? 221Pieces of Intents 222Intent Routing 222Stating Your Intent(ions) 223Narrow Receivers 224The Pause Caveat 225■Chapter 22: Launching Activities and Subactivities 227Peers and Subs 227Start ’Em Up 228Make an Intent 228Make the Call 228Tabbed Browsing, Sort Of 232■Chapter 23: Working with Resources 235The Resource Lineup 235String Theory 235Plain Strings 236String Formats 236Styled Text 237Styled Text and Formats 237Got the Picture? 240XML: The Resource Way 241Miscellaneous Values 243Dimensions 244Colors 244Arrays 245Different Strokes for Different Folks 246RTL Languages: Going Both Ways 250■Chapter 24: Defining and Using Styles 251Styles: DIY DRY 251Elements of Style 253Where to Apply a Style 253The Available Attributes 254Inheriting a Style 254The Possible Values 255Themes: A Style by Any Other Name 256 ■Chapter 25: Handling Multiple Screen Sizes 257Taking the Default 257Whole in One 258Think About Rules, Not Positions . 259Consider Physical Dimensions . 260Avoid “Real” Pixels . 260Choose Scalable Drawables . 260Tailor-Made, Just for You (and You, and You, and ) 261Adding the <supports-screens> Element 261Resources and Resource Sets . 262Finding Your Size . 263Ain’t Nothing Like the Real Thing . 263Density Differs 264Adjusting the Density . 264Ruthlessly Exploiting the Situation . 265Replace Menus with Buttons . 265Replace Tabs with a Simple Activity . 266Consolidate Multiple Activities . 266Example: EU4You 266The First Cut 267Fixing the Fonts 272Fixing the Icons 274Using the Space 274What If It Is Not a Browser? . 276Part III: Honeycomb and Tablets . 279■Chapter 26: Introducing the Honeycomb UI . 281Why Honeycomb? 281What the User Sees 282The Holographic Theme . 285Dealing with the Rest of the Devices . 286■Chapter 27: Using the Action Bar 289Enabling the Action Bar . 289Promoting Menu Items to the Action Bar 290Responding to the Logo . 291Adding Custom Views to the Action Bar 291Defining the Layout . 292Putting the Layout in the Menu . 293Getting Control of User Input . 294Don’t Forget the Phones! . 295■Chapter 28: Fragments 297Introducing Fragments . 297The Problem Addressed by Fragments . 297The Fragments Solution . 298The Android Compatibility Library . 299Creating Fragment Classes . 300General Fragments . 300ListFragment 301 Other Fragment Base Classes 306Fragments, Layouts, Activities, and Multiple Screen Sizes 306EU4You 307DetailsActivity 311Fragments and Configuration Changes 312Designing for Fragments 312■Chapter 29: Handling Platform Changes 313Things That Make You Go Boom 313View Hierarchy 313Changing Resources 314Handling API Changes 314Minimum, Maximum, Target, and Build Versions 315Detecting the Version 316Wrapping the API 317Patterns for Honeycomb 318The Action Bar 319Writing Tablet-Only Apps 321■Chapter 30: Accessing Files 323You and the Horse You Rode in On 323Readin’ ’n Writin’ 326External Storage: Giant Economy-Size Space 330Where to Write 330When to Write 331StrictMode: Avoiding Janky Code 331Setting Up StrictMode 332Seeing StrictMode in Action 332Development Only, Please! 333Conditionally Being Strict 333Linux File Systems: You Sync, You Win 335Part IV: Data Stores, Network Services, and APIs 337■Chapter 31: Using Preferences 339Getting What You Want 339Stating Your Preference 340Introducing PreferenceActivity 340Letting Users Have Their Say 341Adding a Wee Bit o’ Structure 345The Kind of Pop-Ups You Like 347Preferences via Fragments 350The Honeycomb Way 351Adding Backward Compatibility 354■Chapter 32: Managing and Accessing Local Databases 357A Quick SQLite Primer 359Start at the Beginning 359Setting the Table 362Makin’ Data 362What Goes Around, Comes Around 364 Raw Queries 364Regular Queries 364Using Cursors 365Custom CursorAdapters 366Making Your Own Cursors 366Flash: Sounds Faster Than It Is 367Data, Data, Everywhere 367■Chapter 33: Leveraging Java Libraries 369Ants and JARs 369The Outer Limits 370Following the Script 371Reviewing the Script 374■Chapter 34: Communicating via the Internet 377REST and Relaxation 377HTTP Operations via Apache HttpClient 378Parsing Responses 379Stuff to Consider 381AndroidHttpClient 382Leveraging Internet-Aware Android Components 382Downloading Files 383Continuing Our Escape from Janky Code 391Part V: Services 393■Chapter 35: Services: The Theory 395Why Services? 395Setting Up a Service 396Service Class 396Lifecycle Methods 396Manifest Entry 397Communicating to Services 397Sending Commands with startService() 397Binding with bindService() 398Communicating from Services 399Callback/Listener Objects 400Broadcast Intents 400Pending Results 400Messenger 401Notifications 401■Chapter 36: Basic Service Patterns 403The Downloader 403The Design 403The Service Implementation 404Using the Service 406The Music Player 407The Design 407The Service Implementation 408Using the Service 409The Web Service Interface 410[...]... . 531 Explicit Feature Requests 531 Implied Feature Requests 532 A Guaranteed Market 533 Other Stuff That Varies 534 Bugs, Bugs, Bugs 534 Device Testing 535 ■Chapter 49: Where Do We Go from Here? 537 Questions, Sometimes with Answers 537 Heading to the Source 538 Getting... trivial) Android application The process differs depending on whether you are using Eclipse or the command line Eclipse From the Eclipse main menu, choose File ➤ New ➤ Project to open the New Project dialog box, which gives you a list of project type wizards to choose from Expand the Android option and click Android Project, as shown in Figure 3 1 23 24 CHAPTER 3: Your First Android Project Figure 3 1 Selecting... ■Chapter 37 : Alerting Users via Notifications 4 23 Notification Configuration 4 23 Hardware Notifications 424 Icons 424 Notifications in Action 425 Staying in the Foreground 429 FakePlayer, Redux 430 Notifications and Honeycomb . 431 Part VI: Other Android Capabilities 435 ■Chapter... knowledge is to read Learn Java for Android Development by Jeff Friesen (Apress, 2010) Step 2: Install the Android SDK The Android SDK gives you all the tools you need to create and test Android applications It comes in two parts: the base tools, and version-specific SDKs and related add-ons Install the Base Tools You can find the Android developer tools on the Android Developers web site Download... necessary to run an Android emulator To address this, click the Available packages option on the left to open the screen shown in Figure 2–2 9 10 CHAPTER 2: How to Get Started Figure 2–2 Android SDK and AVD Manager available packages Open the Android Repository branch of the tree After a short pause, you will see a screen similar to Figure 2 3 Figure 2 3 Android SDK and AVD Manager available Android packages... 5 03 Signing and Distribution 5 03 Updates 5 03 Issues You May Encounter 5 03 Android Device Versions 5 03 Screen Sizes and Densities 504 Limited Platform Integration 504 Performance and Battery .505 Look and Feel .505 Distribution 505 HTML5 and Alternative Android. .. will go through a few startup phases, the first of which displays a plain-text ANDROID label, as shown in Figure 2–12 17 18 CHAPTER 2: How to Get Started Figure 2–12 Android emulator, initial startup segment The second phase displays a graphical Android logo, as shown in Figure 2– 13 CHAPTER 2: How to Get Started Figure 2– 13 Android emulator, secondary startup segment Finally, the emulator reaches the... 38 : Requesting and Requiring Permissions 437 Mother, May I? 437 Halt! Who Goes There? . 438 Enforcing Permissions via the Manifest . 439 Enforcing Permissions Elsewhere 440 May I See Your Documents? .440 New Permissions in Old Applications 440 Permissions: Up Front or Not at All .441 ■Chapter 39 :... Your News Fix . 539 Index 541 Part Core Concept I Chapter 1 The Big Picture Android is everywhere Phones Tablets TVs and set-top boxes powered by Google TV Soon, Android will be in cars and all sort of other places as well However, the general theme of Android devices will be smaller screens and/or no hardware keyboard And, by the numbers, Android will probably be associated... Platform” for all Android SDK releases you want to test against “Documentation for Android SDK” for the latest Android SDK release “Samples for SDK” for the latest Android SDK release, and perhaps for older releases if you wish Then, open the Third party Add-ons branch of the tree After a short pause, you will see a screen similar to Figure 2–4 CHAPTER 2: How to Get Started Figure 2–4 Android SDK and . Write 33 0 When to W rite 33 1 StrictMode: Avoiding Janky Code 33 1 Setting Up StrictM ode 33 2 Seeing StrictMode in Action 33 2 Development Only, Please! 33 3 Conditionally Being Strict 33 3 Linux. You W in 33 5 Part IV: Data Stores, Network Services, and APIs 33 7 ■Chapter 31 : Using Preferences 33 9 Getting What You Want 33 9 Stating Your Preference 34 0 Introducing PreferenceActivity 34 0 Letting. 37 9 Stuff to Consider 38 1 AndroidHttpClient 38 2 Leveraging Internet-Aware Android Components 38 2 Downloading Files 38 3 Continuing Our Escape from Janky Code 39 1 Part V: Services 39 3 ■Chapter
- Xem thêm -

Xem thêm: apress beginning android 3 (2011), apress beginning android 3 (2011), apress beginning android 3 (2011), Grid Your Lions (or Something Like That...), Tailor-Made, Just for You (and You, and You, and...)

Mục lục

Xem thêm

Gợi ý tài liệu liên quan cho bạn

Nhận lời giải ngay chưa đến 10 phút Đăng bài tập ngay