1. Trang chủ
  2. » Công Nghệ Thông Tin

apress android recipes, a problem-solution approach (2011)

442 2,1K 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 442
Dung lượng 8,14 MB

Nội dung

i Android Recipes A Problem-Solution Approach ■ ■ ■ Dave Smith Jeff Friesen Android Recipes: A Problem-Solution Approach Copyright © 2011 by Dave Smith and Jeff Friesen 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-3413-5 ISBN-13 (electronic): 978-1-4302-3414-2 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: Tom Welsh Technical Reviewer: Paul Connolly Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Corbin Collins Copy Editor: Tracy Brown Compositor: MacPS, LLC Indexer: BIM Indexing & Proofreading Services 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/info/bulksales. 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 ■Chapter 1: Getting Started with Android 1 What Is Android? 1 History of Android 2 Android Architecture 3 App Architecture 7 Components 7 Intents 9 Manifest 11 App Package 13 Activities in Depth 13 Services in Depth 19 Broadcast Receivers in Depth . 25 Content Providers in Depth . 26 1–1. Installing the Android SDK . 27 1–2. Installing an Android Platform . 30 1–3. Creating an Android Virtual Device . 34 1–4. Starting the AVD . 36 1–5. Introducing UC . 41 1–6. Creating UC 53 1–7. Installing and Running UC . 55 1–8. Preparing UC for Publishing . 59 1–9. Migrating to Eclipse . 63 1–10. Developing UC with Eclipse . 66 Summary 72 T ■Chapter 2: User Interface Recipes 75 2–1. Customizing the Window 75 2–2. Creating and Displaying Views 83  2–3. Monitoring Click Actions 85 2–4. Resolution-Independent Assets 86 2–5. Locking Activity Orientation 88 2–6. Dynamic Orientation Locking 89 2–7. Manually Handling Rotation 91 2–8. Creating Pop-Up Menu Actions 93 2–9. Customizing Options Menu 98 2–10. Customizing Back Button 101 2–11. Emulating the Home Button 102 2–12. Monitoring TextView Changes 103 2–13. Scrolling TextView Ticker 106 2–14. Animating a View 107 2–15. Creating Drawables as Backgrounds 115 2–16. Creating Custom State Drawables 119 2–17. Applying Masks to Images 121 2–18. Creating Dialogs that Persist 125 2–19. Implementing Situation-Specific Layouts 127 2–20. Customizing Keyboard Actions 132 2–21. Dismissing Soft Keyboard 135 2–22. Customizing AdapterView Empty Views 136 2–23. Customizing ListView Rows 138 2–24. Making ListView Section Headers 142 2–25. Creating Compound Controls 145 Useful Tools to Know: DroidDraw 148 Obtaining and Launching DroidDraw 149 Exploring DroidDraw’s User Interface 150 Creating a Simple Screen 151 Summary 153 ■Chapter 3: Communications and Networking 155 3–1. Displaying Web Information 155  3–2. Intercepting WebView Events 159 3–3. Accessing WebView with JavaScript 161 3–4. Downloading an Image File 163 3–5. Downloading Completely in the Background 166 3–6. Accessing a REST API 170 3–7. Parsing JSON 176 3–8. Parsing XML 180 3–8. Receiving SMS 184 3–9. Sending an SMS Message 186 3–10. Communicating over Bluetooth 188  3–11. Querying Network Reachability 197 Summary 198 ■Chapter 4: Interacting with Device Hardware and Media 201 4–1. Integrating Device Location 201 4–2. Mapping Locations 205 4–3. Annotating Maps 209 4–4. Capturing Images and Video 216 4–5. Making a Custom Camera Overlay 221 4–6. Recording Audio 227 4–7. Adding Speech Recognition 229 4–8. Playing Back Audio/Video 231 4–9. Creating a Tilt Monitor 240 4–10. Monitoring Compass Orientation 243 Useful Tools to Know: SensorSimulator 247 Obtaining Sensor Simulator 248 Launching Sensor Simulator Settings and Sensor Simulator 248 Accessing Sensor Simulator from Your Apps 253 Summary 255 ■Chapter 5: Persisting Data 257 5–1. Making a Preference Screen 257 5–2. Persisting Simple Data 262 5–3. Reading and Writing Files 266 5–4. Using Files as Resources 271 5–5. Managing a Database 274 5–6. Querying a Database 279 5–7. Backing Up Data 281 5–8. Sharing Your Database 286 5–9. Sharing Your Other Data 292 Useful Tools to Know: SQLite3 298 SQLite3 and UC 300 Summary 308 ■Chapter 6: Interacting with the System 309 6–1. Notifying from the Background 309 6–2. Creating Timed and Periodic Tasks 313 6–3. Scheduling a Periodic Task 315 6–4. Creating Sticky Operations 319 6–5. Running Persistent Background Operations 324 6–6. Launching Other Applications 330 6–7. Launching System Applications 333  6–8. Letting Other Applications Launch Yours 338 6–9. Interacting with Contacts 340 6–10. Picking Device Media 347 6–11. Saving to the MediaStore 349 Summary 352 ■Chapter 7: Working with Libraries 353 7–1. Creating Java Library JARs 353 7–2. Using Java Library JARs 356 7–3. Creating Android Library Projects 359 7–4. Using Android Library Projects 363 7–5. Charting 366 7–6. Practical Push Messaging 375 Summary 384 ■Appendix A: Scripting Layer for Android 385 Installing SL4A 385 Exploring SL4A 386 Adding a Shell Script 387 Accessing the Linux Shell 388 Installing the Python Interpreter 389 Scripting with Python 394 ■Appendix B: Android NDK 397 Installing the NDK 397  Exploring the NDK 400 Greetings from the NDK 401 Building and Running NDKGreetings with the Android SDK 403 Building and Running NDKGreetings with Eclipse 406 Sampling the NDK 407 ■Appendix C: App Design Guidelines 411 C–1. Designing Filtered Apps 411 C–2. Designing High-Performance Apps 413 C–3. Designing Responsive Apps 415 C–4. Designing Seamless Apps 416  Index 419 1 1 Chapter Getting Started with Android Android is hot, and many people are developing Android applications (apps for short). Perhaps you would also like to develop apps, but are unsure about how to get started. Although you could study Google’s online Android Developer’s Guide (http://developer.android.com/guide/index.html) to acquire the needed knowledge, you might be overwhelmed by the vast amount of information that this guide presents. In contrast, this chapter provides just enough theory to help you understand the basics of Android. This theory is followed by several recipes that teach you how to develop apps and prepare them for publication to Google’s Android Market. What Is Android? The Android Developer’s Guide defines Android as a software stack – a set of software subsystems needed to deliver a fully functional solution – for mobile devices. This stack includes an operating system (a modified version of the Linux kernel), middleware (software that connects the low-level operating system to high-level apps) that’s partly based on Java, and key apps (written in Java) such as a web browser (known as Browser) and a contact manager (known as Contacts). Android offers the following features:  Application framework enabling reuse and replacement of app components (discussed later in this chapter)  Bluetooth, EDGE, 3G, and WiFi support (hardware dependent)  Camera, GPS, compass, and accelerometer support (hardware dependent)  Dalvik Virtual Machine (DVM) optimized for mobile devices  GSM Telephony support (hardware dependent) 1 CHAPTER 1: Getting Started with Android 2  Integrated browser based on the open source WebKit engine  Media support for common audio, video, and still image formats (MPEG4, H.264, MP3, AAC, AMR, JPG, PNG, GIF)  Optimized graphics powered by a custom 2D graphics library; 3D graphics based on the OpenGL ES 1.0 specification (hardware acceleration optional)  SQLite for structured data storage Although not part of an Android device’s software stack, Android’s rich development environment (including a device emulator and a plugin for the Eclipse IDE) could also be considered an Android feature. History of Android Contrary to what you might expect, Android did not originate with Google. Instead, Android was initially developed by Android, Inc., a small Palo Alto, California-based startup company. Google bought this company in July 2005 and released a preview version of the Android SDK in November 2007. In mid-August, 2008, Google released the Android 0.9 SDK beta, and subsequently released the Android 1.0 SDK one month later. Table 1–1 outlines subsequent SDK update releases. (Starting with version 1.5, each major release comes under a code name that’s based on a dessert item.) Table 1–1. Android Update Releases SDK Update Release Date and Changes 1.1 Google released SDK 1.1 on February 9, 2009. Changes included paid apps (via Android Market) and “search by voice” support. 1.5 (Cupcake) Based on Linux Kernel 2.6.27 Google released SDK 1.5 on April 30, 2009. Changes included the ability to record and watch videos through camcorder mode, the ability to upload videos to YouTube and pictures to Picasa, the ability to populate the home screen with widgets, and animated screen transitions. 1.6 (Donut) Based on Linux Kernel 2.6.29 Google released SDK 1.6 on September 15, 2009. Changes included an improved Android Market experience, an integrated camera/camcorder/gallery interface, updated “search by voice” with speed and other improvements, and an updated search experience. 2.0/2.1 (Eclair) Based on Linux Kernel 2.6.29 Google released SDK 2.0 on October 26, 2009. Changes included a revamped user interface, a new contacts list, support for Microsoft Exchange, digital zoom, improved Google Maps (version 3.1.2), HTML5 support for the Browser app, live wallpapers, and Bluetooth 2.1 support. Google subsequently released SDK update 2.0.1 on December 3, 2009, and SDK update 2.1 on January 12, 2010. CHAPTER 1: Getting Started with Android 3 SDK Update Release Date and Changes 2.2 (Froyo) Based on Linux Kernel 2.6.32 Google released SDK 2.2 on May 20, 2009. Changes included the integration of Chrome’s V8 JavaScript engine into the Browser app, voice dialing and contact sharing over Bluetooth, Adobe Flash 10.1 support, additional app speed improvements courtesy of a JIT implementation, and USB tethering and WiFi hotspot functionality. 2.3 (Gingerbread) Based on Linux Kernel 2.6.35.7 Google released SDK 2.3 on December 6, 2010. Changes included a new concurrent garbage collector that improves an app’s responsiveness, support for gyroscope sensing, support for WebM video playback and other video improvements, support for near field communication, and improved social networking features. This book focuses on Android 2.3. Google subsequently released SDK 2.3.1 to fix some bugs, and SDK 2.3.3, a small feature release that adds several improvements and APIs to the Android 2.3 platform. 3.0 (Honeycomb) Based on Linux 2.6.36 Google released SDK 3.0 on February 22, 2011. Unlike previous releases, version 3.0 focuses exclusively on tablets, such as Motorola Zoom, the first tablet to be released (on February 24, 2011). In addition to an improved user interface, version 3.0 improves multitasking, supports multicore processors, supports hardware acceleration, and provides a 3D desktop with redesigned widgets. Android Architecture The Android software stack consists of apps at the top, middleware (consisting of an application framework, libraries, and the Android runtime) in the middle, and a Linux kernel with various drivers at the bottom. Figure 1–1 shows this layered architecture. CHAPTER 1: Getting Started with Android 4 Figure 1–1. Android’s layered architecture consists of several major parts. Users care about apps, and Android ships with a variety of useful core apps, which include Browser, Contacts, and Phone. All apps are written in the Java programming language. Apps form the top layer of Android’s architecture. Directly beneath the app layer is the application framework, a set of high-level building blocks for creating apps. The application framework is preinstalled on Android devices and consists of the following components:  Activity Manager: This component provides an app’s lifecycle and maintains a shared activity stack for navigating within and among apps. Both topics are discussed later in this chapter.  Content Providers: These components encapsulate data (such as the Browser app’s bookmarks) that can be shared among apps.  Location Manager: This component makes it possible for an Android device to be aware of its physical location. [...]... developing Android apps; you must also install at least one Android platform Solution Use the SDK Manager tool to install an Android platform How It Works Run SDK Manager This tool presents the Android SDK and AVD Manager dialog box, followed by the Refresh Sources and Choose Packages to Install dialog boxes Android SDK and AVD Manager identifies virtual devices, installed packages, and available packages... package="com.example.project" > ... the following tag values: ’s android: name attribute is assigned "android. intent.action.VIEW" ’s android: mimeType attribute is assigned "image/jpeg" MIME type – additional attributes (such as android: path) would typically be present to locate the data to be viewed ’s android: name attribute is assigned "android. intent.category.DEFAULT" to allow the activity to be launched without... includes an activity for dialing a phone number, and its Calculator app includes an activity for performing basic calculations (see Figure 1–2) Figure 1–2 The main activity of Android s Calculator app lets the user perform basic calculations Although an app can include a single activity, it’s more typical for apps to include multiple activities For example, Calculator also includes an “advanced panel” activity... architecture of an Android app differs from desktop application architecture App architecture is based upon components that communicate with each other by using intents that are described by a manifest and that are stored in an app package Components An app is a collection of components (activities, services, content providers, and broadcast receivers) that run in a Linux process and that are managed by Android. .. deletes, and views Android Virtual Devices (discussed in Recipe 1–3) emulator: Runs a full Android software stack down to the kernel level, and includes a set of preinstalled apps (such as Browser) that you can access sqlite3: Manages SQLite databases created by Android apps zipalign: Performs archive alignment optimization on APK files 1–2 Installing an Android Platform Problem Installing the Android. .. NOTE: tags are nested within tags They appear at the same level as the tag At app-install time, permissions requested by the app (via ) are granted to it by Android s package installer, based upon checks against the digital signatures of the apps declaring those permissions and/or interaction... have a single entry point (no C-style main() function, for example) Instead, apps use components that are instantiated and run as needed Activities An activity is a component that presents a user interface so that the user can interact with an app For example, Android s Contacts app includes an activity for entering a 7 8 CHAPTER 1: Getting Started with Android new contact, its Phone app includes an... type), and Intent addCategory(String category) methods specify the intent’s action, MIME type, and category They help Android identify SimpleActivity2 as the activity to be launched CHAPTER 1: Getting Started with Android ACTIVITIES, TASKS, AND THE ACTIVITY STACK Android refers to a sequence of related activities as a task and provides an activity stack (also known as history stack or back stack) to . Data 262 5–3. Reading and Writing Files 266 5–4. Using Files as Resources 271 5–5. Managing a Database 274 5–6. Querying a Database 279 5–7. Backing Up Data 281 5–8. Sharing Your Database. considered an Android feature. History of Android Contrary to what you might expect, Android did not originate with Google. Instead, Android was initially developed by Android, Inc., a small Palo Alto,. Contacts, and Phone. All apps are written in the Java programming language. Apps form the top layer of Android s architecture. Directly beneath the app layer is the application framework, a set

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

TỪ KHÓA LIÊN QUAN