Review Android Cookbook, 2nd Edition

Problems and Solutions for Android Developers

Posted by Box XV on June 19, 2019. 9 min read.

✅ Publisher: O’Reilly Media

✅ Year: May 10, 2017

✅ Authors: Ian F. Darwin

✅ Language: English

✅ Pages: 774

✅ Size: 23.3 MB

Nhảy vào và xây dựng các ứng dụng Android với sự trợ giúp của hơn 230 công thức đã được thử nghiệm. Phiên bản thứ hai của cuốn sách công thức nổi tiếng này bao gồm các công thức để làm việc với giao diện người dùng, cử chỉ đa cảm ứng, nhận thức vị trí, dịch vụ web và các tính năng cụ thể của thiết bị như điện thoại, máy ảnh và gia tốc kế. Bạn cũng có được thông tin hữu ích về việc đóng gói ứng dụng của mình cho Google Play Market.

Lý tưởng cho các nhà phát triển quen thuộc với Java, kiến ​​thức cơ bản về Android và API Java SE, cuốn sách này có các công thức được đóng góp bởi hơn ba chục nhà phát triển Android. Mỗi công thức cung cấp một giải pháp rõ ràng và code mẫu bạn có thể sử dụng ngay trong dự án của mình. Trong số rất nhiều chủ đề, cuốn sách nấu ăn này giúp bạn:

  • Bắt đầu với công cụ bạn cần để phát triển và thử nghiệm ứng dụng Android
  • Tạo bố cục với các điều khiển giao diện người dùng Android, các dịch vụ đồ họa và cơ chế pop-up
  • Xây dựng các dịch vụ nhận biết vị trí trên Google Maps và OpenStreetMap
  • Kiểm soát các khía cạnh của âm nhạc, video trên Android và các khả năng đa phương tiện khác
  • Làm việc với gia tốc kế và các cảm biến Android khác
  • Sử dụng các frameworks trò chơi và hoạt hình khác nhau
  • Lưu trữ và truy xuất dữ liệu liên tục trong các tệp và cơ sở dữ liệu nhúng
  • Truy cập các dịch vụ web RESTful với JSON và các định dạng khác
  • Kiểm tra và khắc phục sự cố các thành phần riêng lẻ và toàn bộ ứng dụng của bạn

Mục lục

1. Getting Started

1.1 Understanding the Android Application Architecture

1.2 Understanding the Android Activity Life Cycle

1.3 Learning About Android Releases

1.4 Learning the Java Language

1.5 Creating a “Hello, World” Application from the Command Line

1.6 Creating a “Hello, World” App with Apache Maven

1.7 Choosing an IDE for Android Development

1.8 Setting Up Android Studio

1.9 Installing Platform Editions and Keeping the SDK Updated

1.10 Creating a “Hello, World” App Using Android Studio

1.11 Converting an Eclipse ADT Project to Android Studio

1.12 Preserving History While Converting from Eclipse to Android Studio

1.13 Building an Android Application with both Eclipse and Android Studio

1.14 Setting Up Eclipse with AndMore (Replacing ADT)

1.15 Creating a “Hello, World” Application Using Eclipse

1.16 Installing the Eclipse Marketplace Client in Your Eclipse

1.17 Upgrading a Project from Eclipse ADT to Eclipse AndMore

1.18 Controlling Emulators/Devices Using Command-Line ADB

1.19 Sharing Java Classes from Another Eclipse Project

1.20 Referencing Libraries to Implement External Functionality

1.21 Using New Features on Old Devices via the Compatibility Libraries

1.22 Using SDK Samples to Help Avoid Head Scratching

1.23 Taking a Screenshot/Video from the Emulator/Android Device

1.24 Program: A Simple CountDownTimer Example

1.25 Program: Tipster, a Tip Calculator for the Android OS

2. Designing a Successful Application

2.1 Exception Handling

2.2 Requesting Android Permissions at Runtime

2.3 Accessing Android’s Application Object as a “Singleton”

2.4 Keeping Data When the User Rotates the Device

2.5 Monitoring the Battery Level of an Android Device

2.6 Creating Splash Screens in Android

2.7 Designing a Conference/Camp/Hackathon/Institution App

2.8 Using Google Analytics in an Android Application

2.9 Setting First-Run Preferences

2.10 Formatting Numbers

2.11 Formatting with Correct Plurals

2.12 Formatting the Time and Date for Display

2.13 Simplifying Date/Time Calculations with the Java 8 java.time API

2.14 Controlling Input with KeyListeners

2.15 Backing Up Android Application Data

2.16 Using Hints Instead of Tool Tips

3. Application Testing

3.1 Setting Up an Android Virtual Device (AVD) for App Testing

3.2 Testing on a Wide Range of Devices with Cloud-Based Testing

3.3 Testing with Eclipse and JUnit

3.4 Testing with Android Studio and JUnit

3.5 Testing with Robolectric and JUnit 4

3.6 Testing with ATSL, Espresso, and JUnit 4

3.7 Troubleshooting Application Crashes

3.8 Debugging Using Log.d() and LogCat

3.9 Getting Bug Reports Automatically with Crash Reporting

3.10 Using a Local Runtime Application Log for Analysis of Field Errors or Situations

3.11 Reproducing Activity Life-Cycle Scenarios for Testing

3.12 Keeping Your App Snappy with StrictMode

3.13 Static Code Testing with Android Lint

3.14 Dynamic Testing with the Monkey Program

3.15 Sending Text Messages and Placing Calls Between AVDs

4. Inter-/Intra-Process Communication

4.1 Opening a Web Page, Phone Number, or Anything Else with an Intent

4.2 Emailing Text from a View

4.3 Sending an Email with Attachments

4.4 Pushing String Values Using Intent.putExtra()

4.5 Retrieving Data from a Subactivity Back to Your Main Activity

4.6 Keeping a Background Service Running While Other Apps Are on Display

4.7 Sending/Receiving a Broadcast Message

4.8 Starting a Service After Device Reboot

4.9 Creating a Responsive Application Using Threads

4.10 Using AsyncTask to Do Background Processing

4.11 Sending Messages Between Threads Using an Activity Thread Queue and Handler

4.12 Creating an Android Epoch HTML/JavaScript Calendar

5. Graphics

5.1 Using a Custom Font

5.2 Drawing a Spinning Cube with OpenGL ES

5.3 Adding Controls to the OpenGL Spinning Cube

5.4 Freehand Drawing Smooth Curves

5.5 Taking a Picture Using an Intent

5.6 Taking a Picture Using android.media.Camera

5.7 Scanning a Barcode or QR Code with the Google ZXing Barcode Scanner

5.8 Using AndroidPlot to Display Charts and Graphs

5.9 Using Inkscape to Create an Android Launcher Icon from OpenClipArt.org

5.10 Using Paint.NET to Create Launcher Icons from OpenClipArt.org

5.11 Using Nine Patch Files

5.12 Creating HTML5 Charts with Android RGraph

5.13 Adding a Simple Raster Animation

5.14 Using Pinch to Zoom

6. Graphical User Interface

6.1 Understanding and Following User Interface Guidelines

6.2 Looking Good with Material Design

6.3 Choosing a Layout Manager (a.k.a. ViewGroup) and Arranging Components

6.4 Handling Configuration Changes by Decoupling the View from the Model

6.5 Controlling the Action Bar

6.6 Adding a Share Action to Your Action Bar

6.7 Building Modern UIs with the Fragment API

6.8 Creating a Button and Its Click Event Listener

6.9 Enhancing UI Design Using Image Buttons

6.10 Using a FloatingActionButton

6.11 Wiring Up an Event Listener in Many Different Ways

6.12 Using CheckBoxes and RadioButtons

6.13 Using Card Widgets

6.14 Offering a Drop-Down Chooser via the Spinner Class

6.15 Handling Long-Press/Long-Click Events

6.16 Displaying Text Fields with TextView and EditText

6.17 Constraining EditText Values with Attributes and the TextWatcher Interface

6.18 Implementing AutoCompleteTextView

6.19 Feeding AutoCompleteTextView Using a SQLite Database Query

6.20 Turning Edit Fields into Password Fields

6.21 Changing the Enter Key to “Next” on the Soft Keyboard

6.22 Processing Key-Press Events in an Activity

6.23 Let Them See Stars: Using RatingBar

6.24 Making a View Shake

6.25 Providing Haptic Feedback

6.26 Navigating Different Activities Within a TabView

6.27 Creating a Loading Screen that Will Appear Between Two Activities

6.28 Adding a Border with Rounded Corners to a Layout

6.29 Detecting Gestures in Android

6.30 Creating a Simple App Widget

7. GUI Alerts: Menus, Dialogs, Toasts, Snackbars, and Notifications

7.1 Alerting the User with Toast and Snackbar

7.2 Customizing the Appearance of a Toast

7.3 Creating and Displaying a Menu

7.4 Handling Choice Selection in a Menu

7.5 Creating a Submenu

7.6 Creating a Pop-up/Alert Dialog

7.7 Using a Timepicker Widget

7.8 Creating an iPhone-like WheelPicker for Selection

7.9 Creating a Tabbed Dialog

7.10 Creating a ProgressDialog

7.11 Creating a Custom Dialog with Buttons, Images, and Text

7.12 Creating a Reusable “About Box” Class

7.13 Creating a Notification in the Status Bar

8. Other GUI Elements: Lists and Views

8.1 Building List-Based Applications with RecyclerView

8.2 Building List-Based Applications with ListView

8.3 Creating a “No Data” View for ListViews

8.4 Creating an Advanced ListView with Images and Text

8.5 Using Section Headers in ListViews

8.6 Keeping the ListView with the User’s Focus

8.7 Writing a Custom List Adapter

8.8 Using a SearchView to Search Through Data in a ListView

8.9 Handling Orientation Changes: From ListView Data Values to Landscape Charting

9. Multimedia

9.1 Playing a YouTube Video

9.2 Capturing Video Using MediaRecorder

9.3 Using Android’s Face Detection Capability

9.4 Playing Audio from a File

9.5 Playing Audio Without Interaction

9.6 Using Speech to Text

9.7 Making the Device Speak with Text-to-Speech

10. Data Persistence

10.1 Reading and Writing Files in Internal and External Storage

10.2 Getting File and Directory Information

10.3 Reading a File Shipped with the App Rather than in the Filesystem

10.4 Getting Space Information About the SD Card

10.5 Providing a Preference Activity

10.6 Checking the Consistency of Default Shared Preferences

10.7 Using a SQLite Database in an Android Application

10.8 Performing Advanced Text Searches on a SQLite Database

10.9 Working with Dates in SQLite

10.10 Exposing Non-SQL Data as a SQL Cursor

10.11 Displaying Data with a CursorLoader

10.12 Parsing JSON Using JSONObject

10.13 Parsing an XML Document Using the DOM API

10.14 Storing and Retrieving Data via a Content Provider

10.15 Writing a Content Provider

10.16 Adding a Contact Through the Contacts Content Provider

10.17 Reading Contact Data Using a Content Provider

10.18 Implementing Drag and Drop

10.19 Sharing Files via a FileProvider

10.20 Backing Up Your SQLite Data to the Cloud with a SyncAdapter

10.21 Storing Data in the Cloud with Google Firebase

11. Telephone Applications

11.1 Doing Something When the Phone Rings

11.2 Processing Outgoing Phone Calls

11.3 Dialing the Phone

11.4 Sending Single-part or Multipart SMS Messages

11.5 Receiving an SMS Message

11.6 Using Emulator Controls to Send SMS Messages to the Emulator

11.7 Using Android’s TelephonyManager to Obtain Device Information

12. Networked Applications

12.1 Consuming a RESTful Web Service Using a URLConnection

12.2 Consuming a RESTful Web Service with Volley

12.3 Notifying Your App with Google Cloud Messaging “Push Messaging”

12.4 Extracting Information from Unstructured Text Using Regular Expressions

12.5 Parsing RSS/Atom Feeds Using ROME

12.6 Using MD5 to Digest Clear Text

12.7 Converting Text into Hyperlinks

12.8 Accessing a Web Page Using a WebView

12.9 Customizing a WebView

12.10 Writing an Inter-Process Communication Service

13. Gaming and Animation

13.1 Building an Android Game Using flixel-gdx

13.2 Building an Android Game Using AndEngine

13.3 Processing Timed Keyboard Input

14. Social Networking

14.1 Authenticating Users with OAUTH2

14.2 Integrating Social Networking Using HTTP

14.3 Loading a User’s Twitter Timeline Using HTML or JSON

15. Location and Map Applications

15.1 Getting Location Information

15.2 Accessing GPS Information in Your Application

15.3 Mocking GPS Coordinates on a Device

15.4 Using Geocoding and Reverse Geocoding

15.5 Getting Ready for Google Maps API V2 Development

15.6 Using the Google Maps API V2

15.7 Displaying Map Data Using OpenStreetMap

15.8 Creating Overlays in OpenStreetMap Maps

15.9 Using a Scale on an OpenStreetMap Map

15.10 Handling Touch Events on an OpenStreetMap Overlay

15.11 Getting Location Updates with OpenStreetMap Maps

16. Accelerometer

16.1 Checking for the Presence or Absence of a Sensor

16.2 Using the Accelerometer to Detect Shaking

16.3 Checking Whether a Device Is Facing Up or Down

16.4 Reading the Temperature Sensor

17. Bluetooth

17.1 Enabling Bluetooth and Making the Device Discoverable

17.2 Connecting to a Bluetooth-Enabled Device

17.3 Accepting Connections from a Bluetooth Device

17.4 Implementing Bluetooth Device Discovery

18. System and Device Control

18.1 Accessing Phone Network/Connectivity Information

18.2 Obtaining Information from the Manifest File

18.3 Changing Incoming Call Notification to Silent, Vibrate, or Normal

18.4 Copying Text and Getting Text from the Clipboard

18.5 Using LED-Based Notifications

18.6 Making the Device Vibrate

18.7 Determining Whether a Given Application Is Running

19. All the World’s Not Java: Other Programming Languages and Frameworks

19.1 Learning About Cross-Platform Solutions

19.2 Running Shell Commands from Your Application

19.3 Running Native C/C++ Code with JNI on the NDK

19.4 Getting Started with SL4A, the Scripting Layer for Android

19.5 Creating Alerts in SL4A

19.6 Fetching Your Google Documents and Displaying Them in a ListView Using SL4A

19.7 Sharing SL4A Scripts in QR Codes

19.8 Using Native Handset Functionality from a WebView via JavaScript

19.9 Building a Cross-Platform App with Xamarin

19.10 Creating a Cross-Platform App Using PhoneGap/Cordova

20. All the World’s Not English: Strings and Internationalization

20.1 Internationalizing Application Text

20.2 Finding and Translating Strings

20.3 Handling the Nuances of strings.xml

21. Packaging, Deploying, and Distributing/Selling Your App

21.1 Creating a Signing Certificate and Using It to Sign Your Application

21.2 Distributing Your Application via the Google Play Store

21.3 Distributing Your Application via Other App Stores

21.4 Monetizing Your App with AdMob

21.5 Obfuscating and Optimizing with ProGuard

21.6 Hosting Your App on Your Own Server

21.7 Creating a “Self-Updating” App

21.8 Providing a Link to Other Published Apps in the Google Play Store