实例介绍
【实例简介】
【实例截图】
【核心代码】
Table of Contents Learning Android ........................................................................................................... xix Prerequisites .......................................................................................................... xix What’s New in the Third Edition? ............................................................................. xix How to Use This Book ............................................................................................. xx How This Book Is Organized .................................................................................... xx Challenges ..................................................................................................... xxi Are you more curious? .................................................................................... xxi Code Style ............................................................................................................. xxi Typographical Conventions ...................................................................................... xxii Android Versions ................................................................................................... xxii The Necessary Tools ..................................................................................................... xxiii Downloading and Installing Android Studio .............................................................. xxiii Downloading Earlier SDK Versions ......................................................................... xxiii A Hardware Device ............................................................................................... xxiv 1. Your First Android Application ........................................................................................ 1 App Basics ............................................................................................................... 2 Creating an Android Project ........................................................................................ 3 Navigating in Android Studio ...................................................................................... 8 Laying Out the UI ..................................................................................................... 9 The view hierarchy .......................................................................................... 13 Widget attributes ............................................................................................. 14 Creating string resources ................................................................................... 15 Previewing the layout ....................................................................................... 16 From Layout XML to View Objects ........................................................................... 17 Resources and resource IDs ............................................................................... 18 Wiring Up Widgets .................................................................................................. 21 Getting references to widgets ............................................................................. 22 Setting listeners ............................................................................................... 23 Making Toasts ......................................................................................................... 25 Using code completion ..................................................................................... 26 Running on the Emulator .......................................................................................... 27 For the More Curious: Android Build Process .............................................................. 30 Android build tools .......................................................................................... 31 Challenges .............................................................................................................. 32 Challenge: Customizing t Android Programming viii Adding an Icon ....................................................................................................... 48 Adding resources to a project ............................................................................ 49 Referencing resources in XML .......................................................................... 51 Challenge: Add a Listener to the TextView .................................................................. 52 Challenge: Add a Previous Button .............................................................................. 52 Challenge: From Button to ImageButton ...................................................................... 53 3. The Activity Lifecycle .................................................................................................. 55 Logging the Activity Lifecycle .................................................................................. 57 Making log messages ....................................................................................... 57 Using Logcat .................................................................................................. 59 Exploring the activity lifecycle by example .......................................................... 60 Rotation and the Activity Lifecycle ............................................................................ 63 Device configurations and alternative resources ..................................................... 63 Saving Data Across Rotation ..................................................................................... 68 Overriding onSaveInstanceState(Bundle) .............................................................. 69 The Activity Lifecycle, Revisited ............................................................................... 70 For the More Curious: Current State of Activity Cleanup ................................................ 72 For the More Curious: Log Levels and Methods ........................................................... 73 Challenge: Preventing Repeat Answers ........................................................................ 74 Challenge: Graded Quiz ............................................................................................ 74 4. Debugging Android Apps .............................................................................................. 75 Exceptions and Stack Traces ..................................................................................... 76 Diagnosing misbehaviors .................................................................................. 78 Logging stack traces ........................................................................................ 78 Setting breakpoints .......................................................................................... 80 Using exception breakpoints .............................................................................. 83 Android-Specific Debugging ...................................................................................... 85 Using Android Lint .......................................................................................... 85 Issues with the R class ..................................................................................... 87 Challenge: Exploring the Layout Inspector ................................................................... 88 Challenge: Exploring Allocation Tracking .................................................................... 89 5. Your Second Activity ................................................................................................... 91 Setting Up a Second Activity .................................................................................... 93 Creating a new activity ..................................................................................... 93 A new activity subclass .................................................................................... 96 Declaring activities in the manifest ..................................................................... 97 Adding a cheat button to QuizActivity ................................................................ 98 Starting an Activity .................................................................................................. 99 Communicating with intents ............................................................................ 100 Passing Data Between Activities ............................................................................... 101 Using intent extras ......................................................................................... 102 Getting a result back from a child activity .......................................................... 105 How Android Sees Your Activities ............................................................................ 109 Challenge: Closing Loopholes for Cheaters ................................................................ 112 6. Android SDK Versions and Compatibility ....................................................................... 113 Android SDK Versions ........................................................................................... 113 Compatibility and Android Programming ................................................................... 114 A sane minimum ........................................................................................... 114 Android Programming ix Minimum SDK version ................................................................................... 117 Target SDK version ........................................................................................ 117 Compile SDK version ..................................................................................... 117 Adding code from later APIs safely .................................................................. 118 Using the Android Developer Documentation ............................................................. 120 Challenge: Reporting the Build Version ..................................................................... 122 Challenge: Limited Cheats ....................................................................................... 122 7. UI Fragments and the Fragment Manager ....................................................................... 123 The Need for UI Flexibility ..................................................................................... 124 Introducing Fragments ............................................................................................ 125 Starting CriminalIntent ............................................................................................ 126 Creating a new project .................................................................................... 129 Two types of fragments ................................................................................... 130 Adding dependencies in Android Studio ............................................................ 131 Creating the Crime class ................................................................................. 134 Hosting a UI Fragment ........................................................................................... 136 The fragment lifecycle .................................................................................... 136 Two approaches to hosting .............................................................................. 137 Defining a container view ............................................................................... 137 Creating a UI Fragment .......................................................................................... 139 Defining CrimeFragment’s layout ..................................................................... 139 Creating the CrimeFragment class ..................................................................... 142 Adding a UI Fragment to the FragmentManager .......................................................... 148 Fragment transactions ..................................................................................... 149 The FragmentManager and the fragment lifecycle ................................................ 152 Application Architecture with Fragments ................................................................... 153 The reason all our activities will use fragments ................................................... 154 For the More Curious: Fragments and the Support Library ............................................ 155 For the More Curious: Why Support Fragments Are Superior ........................................ 156 8. Displaying Lists with RecyclerView .............................................................................. 157 Updating CriminalIntent’s Model Layer ..................................................................... 159 Singletons and centralized data storage .............................................................. 159 An Abstract Activity for Hosting a Fragment .............................................................. 161 A generic fragment-hosting layout .................................................................... 161 An abstract Activity class ................................................................................ 162 RecyclerView, Adapter, and ViewHolder .................................................................... 167 ViewHolders and Adapters .............................................................................. 168 Using a RecyclerView .................................................................................... 171 A view to display .......................................................................................... 173 Implementing a ViewHolder and an Adapter ....................................................... 173 Binding List Items ................................................................................................. 176 Responding to Presses ............................................................................................ 178 For the More Curious: ListView and GridView ........................................................... 179 For the More Curious: Singletons ............................................................................. 179 Challenge: RecyclerView ViewTypes ......................................................................... 180 9. Creating User Interfaces with Layouts and Widgets .......................................................... 181 Using the Graphical Layout Tool .............................................................................. 182 Introducing ConstraintLayout ................................................................................... 183 Android Programming x Using ConstraintLayout .................................................................................. 184 The graphical editor ....................................................................................... 186 Making room ................................................................................................ 188 Adding widgets ............................................................................................. 190 ConstraintLayout’s inner workings .................................................................... 194 Editing properties ........................................................................................... 195 Making list items dynamic .............................................................................. 199 More on Layout Attributes ...................................................................................... 200 Screen pixel densities and dp and sp ................................................................. 200 Margins vs padding ........................................................................................ 202 Styles, themes, and theme attributes .................................................................. 203 Android’s design guidelines ............................................................................. 204 The Graphical Layout Tools and You ........................................................................ 204 Challenge: Formatting the Date ................................................................................ 204 10. Using Fragment Arguments ........................................................................................ 205 Starting an Activity from a Fragment ........................................................................ 205 Putting an extra ............................................................................................. 207 Retrieving an extra ......................................................................................... 207 Updating CrimeFragment’s view with Crime data ................................................ 208 The downside to direct retrieval ........................................................................ 209 Fragment Arguments .............................................................................................. 210 Attaching arguments to a fragment .................................................................... 210 Retrieving arguments ...................................................................................... 211 Reloading the List .................................................................................................. 212 Getting Results with Fragments ................................................................................ 215 For the More Curious: Why Use Fragment Arguments? ................................................ 216 Challenge: Efficient RecyclerView Reloading ............................................................. 216 Challenge: Improving CrimeLab Performance ............................................................. 216 11. Using ViewPager ...................................................................................................... 217 Creating CrimePagerActivity .................................................................................... 219 ViewPager and PagerAdapter ........................................................................... 219 Integrating CrimePagerActivity ......................................................................... 221 FragmentStatePagerAdapter vs FragmentPagerAdapter ................................................. 223 For the More Curious: How ViewPager Really Works .................................................. 224 For the More Curious: Laying Out Views in Code ....................................................... 225 Challenge: Restoring CrimeFragment’s Margins .......................................................... 226 Challenge: Adding First and Last Buttons .................................................................. 226 12. Dialogs ................................................................................................................... 227 Creating a DialogFragment ...................................................................................... 228 Showing a DialogFragment .............................................................................. 231 Setting a dialog’s contents ............................................................................... 232 Passing Data Between Two Fragments ....................................................................... 235 Passing data to DatePickerFragment .................................................................. 236 Returning data to CrimeFragment ..................................................................... 238 Challenge: More Dialogs ......................................................................................... 245 Challenge: A Responsive DialogFragment .................................................................. 245 13. The Toolbar ............................................................................................................. 247 AppCompat ........................................................................................................... 248 Android Programming xi Using the AppCompat library .......................................................................... 248 Menus .................................................................................................................. 250 Defining a menu in XML ................................................................................ 250 Creating the menu .......................................................................................... 256 Responding to menu selections ......................................................................... 259 Enabling Hierarchical Navigation ............................................................................. 260 How hierarchical navigation works ................................................................... 261 An Alternative Action Item ..................................................................................... 262 Toggling the action item title ........................................................................... 263 “Just one more thing...” ................................................................................... 265 For the More Curious: Action Bar vs Toolbar ............................................................. 267 Challenge: Deleting Crimes ..................................................................................... 267 Challenge: Plural String Resources ........................................................................... 268 Challenge: An Empty View for the RecyclerView ........................................................ 268 14. SQLite Databases ..................................................................................................... 269 Defining a Schema ................................................................................................. 270 Building Your Initial Database ................................................................................. 271 Exploring files using Android Device Monitor .................................................... 274 Debugging database issues .............................................................................. 276 Gutting CrimeLab .................................................................................................. 277 Writing to the Database .......................................................................................... 278 Using ContentValues ...................................................................................... 278 Inserting and updating rows ............................................................................. 279 Reading from the Database ...................................................................................... 281 Using a CursorWrapper .................................................................................. 282 Converting to model objects ............................................................................ 284 For the More Curious: More Databases ..................................................................... 287 For the More Curious: The Application Context .......................................................... 288 Challenge: Deleting Crimes ..................................................................................... 288 15. Implicit Intents ......................................................................................................... 289 Adding Buttons ..................................................................................................... 290 Adding a Suspect to the Model Layer ....................................................................... 291 Using a Format String ............................................................................................ 293 Using Implicit Intents ............................................................................................. 294 Parts of an implicit intent ................................................................................ 295 Sending a crime report .................................................................................... 296 Asking Android for a contact ........................................................................... 299 Checking for responding activities .................................................................... 302 Challenge: ShareCompat ......................................................................................... 304 Challenge: Another Implicit Intent ............................................................................ 304 16. Taking Pictures with Intents ....................................................................................... 305 A Place for Your Photo ........................................................................................... 305 File Storage .......................................................................................................... 308 Using FileProvider ......................................................................................... 309 Designating a picture location .......................................................................... 310 Using a Camera Intent ............................................................................................ 311 Firing the intent ............................................................................................. 311 Scaling and Displaying Bitmaps ............................................................................... 313 Android Programming xii Declaring Features ................................................................................................. 316 Challenge: Detail Display ........................................................................................ 316 Challenge: Efficient Thumbnail Load ........................................................................ 316 17. Two-Pane Master-Detail Interfaces ............................................................................... 317 Adding Layout Flexibility ....................................................................................... 319 Modifying SingleFragmentActivity ................................................................... 320 Creating a layout with two fragment containers ................................................... 321 Using an alias resource ................................................................................... 323 Creating tablet alternatives ............................................................................... 324 Activity: Fragment Boss .......................................................................................... 325 Fragment callback interfaces ............................................................................ 326 For the More Curious: More on Determining Device Size ............................................. 335 Challenge: Adding Swipe to Dismiss ........................................................................ 335 18. Localization ............................................................................................................. 337 Localizing Resources .............................................................................................. 338 Default resources ........................................................................................... 341 Checking string coverage using Translations Editor .............................................. 344 Targeting a region .......................................................................................... 345 Configuration Qualifiers .......................................................................................... 348 Prioritizing alternative resources ....................................................................... 349 Multiple qualifiers .......................................................................................... 351 Finding the best-matching resources .................................................................. 352 Testing Alternative Resources .................................................................................. 353 Challenge: Localizing Dates .................................................................................... 354 19. Accessibility ............................................................................................................ 355 TalkBack .............................................................................................................. 355 Explore by Touch .......................................................................................... 359 Linear navigation by swiping ........................................................................... 359 Making Non-Text Elements Readable by TalkBack ...................................................... 362 Adding content descriptions ............................................................................. 362 Making a widget focusable .............................................................................. 364 Creating a Comparable Experience ........................................................................... 365 Using labels to provide context ........................................................................ 367 For the More Curious: Using Accessibility Scanner ..................................................... 370 Challenge: Improving the List .................................................................................. 373 Challenge: Providing Enough Context for Data Entry ................................................... 373 Challenge: Announcing Events ................................................................................. 373 20. Data Binding and MVVM .......................................................................................... 375 Different Architectures: Why Bother? ........................................................................ 376 Creating BeatBox ................................................................................................... 376 Simple data binding ....................................................................................... 378 Importing Assets .................................................................................................... 382 Getting At Assets ................................................................................................... 384 Wiring Up Assets for Use ....................................................................................... 386 Binding to Data ..................................................................................................... 389 Creating a ViewModel .................................................................................... 391 Binding to a ViewModel ................................................................................. 392 Observable data ............................................................................................. 394 Android Programming xiii Accessing Assets ................................................................................................... 396 For the More Curious: More About Data Binding ........................................................ 396 Lambda expressions ....................................................................................... 397 More syntactic sugar ...................................................................................... 397 BindingAdapters ............................................................................................ 397 For the More Curious: Why Assets, Not Resources ...................................................... 398 For the More Curious: Non-Assets? .......................................................................... 398 21. Unit Testing and Audio Playback ................................................................................ 399 Creating a SoundPool ............................................................................................. 399 Loading Sounds ..................................................................................................... 400 Playing Sounds ...................................................................................................... 402 Test Dependencies .................................................................................................. 402 Creating a Test Class .............................................................................................. 403 Setting Up Your Test .............................................................................................. 405 Using mocked dependencies ............................................................................ 405 Writing Tests ......................................................................................................... 407 Testing object interactions ............................................................................... 407 Data Binding Callbacks .......................................................................................... 411 Unloading Sounds .................................................................................................. 412 Rotation and Object Continuity ................................................................................ 413 Retaining a fragment ...................................................................................... 414 Rotation and retained fragments ....................................................................... 414 For the More Curious: Whether to Retain .................................................................. 416 For the More Curious: Espresso and Integration Testing ................................................ 417 For the More Curious: Mocks and Testing .................................................................. 418 Challenge: Playback Speed Control ........................................................................... 419 22. Styles and Themes .................................................................................................... 421 Color Resources .................................................................................................... 422 Styles ................................................................................................................... 422 Style inheritance ............................................................................................ 424 Themes ................................................................................................................ 425 Modifying the theme ...................................................................................... 426 Adding Theme Colors ............................................................................................ 428 Overriding Theme Attributes .................................................................................... 429 Theme spelunking .......................................................................................... 429 Modifying Button Attributes .................................................................................... 433 For the More Curious: More on Style Inheritance ........................................................ 435 For the More Curious: Accessing Theme Attributes ..................................................... 436 23. XML Drawables ....................................................................................................... 437 Making Uniform Buttons ........................................................................................ 438 Shape Drawables ................................................................................................... 439 State List Drawables ............................................................................................... 441 Layer List Drawables ............................................................................................. 443 For the More Curious: Why Bother with XML Drawables? ........................................... 444 For the More Curious: Mipmap Images ..................................................................... 445 For the More Curious: 9-Patch Images ...................................................................... 446 Challenge: Button Themes ....................................................................................... 452 24. More About Intents and Tasks .................................................................................... 453 Android Programming xiv Setting Up NerdLauncher ........................................................................................ 454 Resolving an Implicit Intent ..................................................................................... 456 Creating Explicit Intents at Runtime .......................................................................... 460 Tasks and the Back Stack ........................................................................................ 462 Switching between tasks ................................................................................. 462 Starting a new task ......................................................................................... 464 Using NerdLauncher as a Home Screen ..................................................................... 466 Challenge: Icons .................................................................................................... 468 For the More Curious: Processes vs Tasks .................................................................. 468 For the More Curious: Concurrent Documents ............................................................ 471 25. HTTP and Background Tasks ..................................................................................... 473 Creating PhotoGallery ............................................................................................ 475 Networking Basics ................................................................................................. 477 Asking permission to network .......................................................................... 479 Using AsyncTask to Run on a Background Thread ...................................................... 479 You and Your Main Thread ..................................................................................... 481 Beyond the main thread .................................................................................. 482 Fetching JSON from Flickr ..................................................................................... 483 Parsing JSON text .......................................................................................... 487 From AsyncTask Back to the Main Thread ................................................................. 491 Cleaning Up AsyncTasks ........................................................................................ 495 For the More Curious: More on AsyncTask ................................................................ 496 For the More Curious: Alternatives to AsyncTask ........................................................ 497 Challenge: Gson .................................................................................................... 498 Challenge: Paging .................................................................................................. 498 Challenge: Dynamically Adjusting the Number of Columns .......................................... 498 26. Loopers, Handlers, and HandlerThread ......................................................................... 499 Preparing RecyclerView to Display Images ................................................................ 499 Downloading Lots of Small Things ........................................................................... 502 Communicating with the Main Thread ....................................................................... 502 Assembling a Background Thread ............................................................................ 504 Messages and Message Handlers .............................................................................. 506 Message anatomy ........................................................................................... 506 Handler anatomy ............................................................................................ 507 Using handlers .............................................................................................. 508 Passing handlers ............................................................................................ 512 For the More Curious: AsyncTasks vs Threads ............................................................ 518 For the More Curious: Solving the Image Downloading Problem .................................... 518 For the More Curious: StrictMode ............................................................................ 519 Challenge: Preloading and Caching ........................................................................... 520 27. Search .................................................................................................................... 521 Searching Flickr .................................................................................................... 521 Using SearchView .................................................................................................. 526 Responding to SearchView user interactions ....................................................... 530 Simple Persistence with Shared Preferences ................................................................ 532 Polishing Your App ................................................................................................ 536 Challenge: Polishing Your App Some More ................................................................ 536 28. Background Services ................................................................................................. 537 Android Programming xv Creating an IntentService ........................................................................................ 537 What Services Are For ........................................................................................... 540 Safe background networking ............................................................................ 540 Looking for New Results ........................................................................................ 542 Delayed Execution with AlarmManager ..................................................................... 544 Being a good citizen: using alarms the right way ................................................. 545 PendingIntent ................................................................................................ 547 Managing alarms with PendingIntent ................................................................. 547 Controlling Your Alarm .......................................................................................... 548 Notifications .......................................................................................................... 551 Challenge: Notifications on Android Wear .................................................................. 553 For the More Curious: Service Details ....................................................................... 553 What a service does (and does not do) ............................................................... 553 A service’s lifecycle ....................................................................................... 554 Non-sticky services ........................................................................................ 554 Sticky services ............................................................................................... 554 Bound services .............................................................................................. 555 For the More Curious: JobScheduler and JobServices ................................................... 556 JobScheduler and the future of background work ................................................. 559 Challenge: Using JobService on Lollipop ................................................................... 559 For the More Curious: Sync Adapters ....................................................................... 559 29. Broadcast Intents ...................................................................................................... 561 Regular Intents vs Broadcast Intents .......................................................................... 561 Receiving a System Broadcast: Waking Up on Boot ..................................................... 562 Creating and registering a standalone receiver ..................................................... 562 Using receivers .............................................................................................. 565 Filtering Foreground Notifications ........................................... Android Programming xvi Setting Up the DragAndDraw Project ........................................................................ 598 Creating a Custom View ......................................................................................... 600 Creating BoxDrawingView .............................................................................. 600 Handling Touch Events ........................................................................................... 602 Tracking across motion events .......................................................................... 604 Rendering Inside onDraw(Canvas) ............................................................................ 606 Challenge: Saving State .......................................................................................... 608 Challenge: Rotating Boxes ...................................................................................... 608 32. Property Animation ................................................................................................... 609 Building the Scene ................................................................................................. 609 Simple Property Animation ..................................................................................... 612 View transformation properties ......................................................................... 614 Using different interpolators ............................................................................ 616 Color evaluation ............................................................................................ 616 Playing Animators Together ..................................................................................... 618 For the More Curious: Other Animation APIs ............................................................. 620 Legacy animation tools ................................................................................... 620 Transitions .................................................................................................... 620 Challenges ............................................................................................................ 620 33. Locations and Play Services ....................................................................................... 621 Locations and Libraries ........................................................................................... 622 Google Play Services ...................................................................................... 622 Creating Locatr ...................................................................................................... 623 Play Services and Location Testing on Emulators ........................................................ 623 Mock location data ......................................................................................... 624 Building Out Locatr ............................................................................................... 627 Setting Up Google Play Services .............................................................................. 629 Location permissions ...................................................................................... 630 Using Google Play Services ..................................................................................... 631 Flickr Geosearch .................................................................................................... 633 Getting a Location Fix ............................................................................................ 634 Asking for Permission at Runtime ............................................................................ 637 Checking for permissions ................................................................................ 638 Find and Display an Image ...................................................................................... 644 Challenge: Permissions Rationale ............................................................................. 646 Challenge: Progress ................................................................................................ 646 34. Maps ...................................................................................................................... 647 Importing Play Services Maps .................................................................................. 647 Mapping on Android .............................................................................................. 647 Getting a Maps API Key ......................................................................................... 648 Setting Up Your Map ............................................................................................. 650 Getting More Location Data .................................................................................... 652 Working with Your Map ......................................................................................... 654 Drawing on the map ....................................................................................... 657 For the More Curious: Teams and API Keys ............................................................... 659 35. Material Design ........................................................................................................ 661 Material Surfaces ................................................................................................... 662 Elevation and Z values .................................................................................... 664 Android Programming xvii State list animators ......................................................................................... 665 Animation Tools .................................................................................................... 667 Circular reveal ............................................................................................... 667 Shared element transitions ............................................................................... 669 View Components .................................................................................................. 673 Cards ........................................................................................................... 673 Floating action buttons .................................................................................... 675 Snackbars ..................................................................................................... 676 More on Material Design ........................................................................................ 677 36. Afterword ................................................................................................................ 679 The Final Challenge ............................................................................................... 679 Shameless Plugs .................................................................................................... 679 Thank You ............................................................................................................ 680 Index ........................................................................................................................... 681
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论