To obtain the source code of the programs presented in the book, click the following link ( Resources ). (Password no longer required)

Sample Chapters:

 

If you find the materials useful, please write a review of this book at Amazon.com.

Table of Contents:

Chapter 1 Introduction    1
  1.1 The Value of Knowledge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
  1.2 The Open-Source Movement . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
  1.3 This Book . . . . . . . .  . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

Chapter 2 An Introduction to Android   5
  2.1 Introduction . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . 5
  2.2 Development Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
    2.2.1 Eclipse IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
  2.3 Android Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
    2.3.1 Manifest File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  11
  2.4 Command Line Development . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
    2.4.1 Listing Targets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  14
    2.4.2 Creating AVDs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  15
    2.4.3 Creating Project . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
  2.5 Simple Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  19
    2.5.1 Button and ImageButton . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
    2.5.2 Interest Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . .  22
    2.5.3 Grid View Demo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
  2.6 Running On a Real Android Device . . . . . . . . . . . . . . . . . . . . . . . . 35
  2.7 Notes on Using Eclipse IDE . . . . . . . . . . . . . . . . . . . . . . . . . . . 37

Chapter 3 Android Components and Simple Examples    39
  3.1 GUI Example with Eclipse IDE . . . . . . . . . . . . . . . . . . . . . . . . . . 39
    3.1.1 Using a Linear Layout . . . . . . . . . . . . . . . . . . . . . . . . . . .. 41
    3.1.2 Responding to Clicking . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
    3.1.3 Creating a New Activity . . . . . . . . . . . . . . . . . . . . . . . . . .. 43
  3.2 GUI Example using Command Line . . . . . . . . . . . . . . . . . . . . . . . . . 46
  3.3 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
    3.3.1 Activity and Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 52
    3.3.2 A Simple Calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 53
  3.4 Fragment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
    3.4.1 Why Fragments? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
    3.4.2 Fragment Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
  3.5 Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. 62
    3.5.1 Android Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
    3.5.2 Local Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
    3.5.3 A Simple Local Service Example . . . . . . . . . . . . . . . . . . . . . . . 65
    3.5.4 Remote Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .68

Chapter 4 Android Graphics and OpenGL ES 1.X    71
  4.1 Graphics Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
    4.1.1 Class View and Subclasses . . . . . . . . . . . . . . . . . . . . . . . . . .71
    4.1.2 Class SurfaceView . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .72
    4.1.3 Class GLSurfaceView . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73
  4.2 OpenGL ES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .75
  4.3 OpenGL ES 1.X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  76
    4.3.1 Creating an Activity with GLSurfaceView . . . . . . . . . . . . . . . . . . .76
    4.3.2 Drawing a Triangle on GLSurfaceView . . . . . . . . . . . . . . . . . . . . .78
    4.3.3 Setting Camera View and Transformations . . . . . . . . . . . . . . . . . . .80
  4.4 Animation and Event Handling . . . . . . . . . . . . . . . . . . . . . . . . . . 81
  4.5 Rendering a 3D Color Cube . . . . . . . . . . . . . . . . . . . . . . . . . .. . 83
    4.5.1 Color Cube . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
    4.5.2 Rendering a Square Only . . . . . . . . . . . . . . . . . . . . . . . . .. . 89
    4.5.3 Rendering a Rotating Cube . . . . . . . . . . . . . . . . . . . . . . . .. . 90
  4.6 Rendering a 3D Texture Cube . . . . . . . . . . . . . . . . . . . . . . . .. . . 92
    4.6.1 Displaying Images . . . . . . . . . . . . . . . . . . . . . . . . . .  . . . 92
    4.6.2 Rendering Texture Square . . . . . . . . . . . . . . . . . . . . . . . . . . 94
    4.6.3 Rendering Texture Cube . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
  4.7 Rendering a Rotating Cube . . . . . . . . . . . . . . . . . . . . . . . . . . . 102

Chapter 5 File I/O and JNI    103
  5.1 Read Raw Data From File . . . . . . . . . . .  . . . . . . . . . . . . . . . . . 103
  5.2 Read and Write Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
    5.2.1 Read Assets and Display Files . . . . . .  . . . . . . . . . . . . . . . . . 104
    5.2.2 Write to Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
    5.2.3 External Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
  5.3 Android JNI . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . 110
    5.3.1 Installing Android NDK . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
    5.3.2 A Simple Example of JNI . . . . . . . . . .. . . . . . . . . . . . . . . . . 110
    5.3.3 A Simple JNI Example with UI . . . . . . . . . . . . . . . . . . . . . . . . 113

Chapter 6 Graphics with OpenGL ES 2.X  117
  6.1 Programmable Pipeline . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . 117
  6.2 OpenGL Shading Language (GLSL) . . . . . . . . . . . . . . . . . . . . . . . . . 118
    6.2.1 OpenGL Shaders Execution Model . . . . . . . . . . . . . . . . . . . . . . . 118
    6.2.2 OpenGL Shading Language API . . . . . . . . . . .  . . . . . . . . . . . . . 120
    6.2.3 Data Types in GLSL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
  6.3 Android Graphics with ES 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . 128
    6.3.1 Drawing a Triangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
    6.3.2 Shaders in Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
    6.3.3 Animation . . . . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . 136
    6.3.4 Drawing a Square . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
    6.3.5 Drawing a Color Square . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
    6.3.6 Temperature Shaders . . . . . . . . . . . . . . .. . . . . . . . . . . . . . 144
  6.4 Drawing 3D Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
    6.4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
    6.4.2 Drawing a Tetrahedron . . . . . . . . . . .  . . . . . . . . . . . . . . . . 149
    6.4.3 Rotating a Color Tetrahedron . . . . . . . . . . . . . . . . . . . . . . . . 153
  6.5 Drawing Spheres . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . 158
    6.5.1 Spherical Coordinates . . . . . . . .  . . . . . . . . . . . . . . . . . . . 158
    6.5.2 Rendering a Wireframe Sphere . . . . . . . . . . . . . . . . . . . . . . . . 159
    6.5.3 Rendering a Color Solid Sphere . . . . . . . . . . . . . . . . . . . . . . . 163
    6.5.4 Lightling a Sphere . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164

Chapter 7 Thread Programming   171
  7.1 Processes and Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
    7.2 Java Threads . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . 172
    7.2.1 Thread Creation by Extending Thread Class . . . . . . . . . . . . . . . . . . 172
    7.2.2 Thread Creation by Implementing Runnable Interface . . . . . . . . . . . . .  173
    7.2.3 Wait for a Thread . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
  7.3 Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
    7.3.1 Mutual Exclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . 177
    7.3.2 Semaphore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
    7.3.3 Producer-Consumer Problem . . . . . . . . . . . . . . . . . . . . . . . . . . 184
    7.3.4 Condition Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  189
    7.3.5 Readers-Writers Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
  7.4 Deadlocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195

Chapter 8 Network Communication  199
  8.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
  8.2 Connecting to a Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
  8.3 Communication Between Android Devices . . .  . . . . . . . . . . . . . . . . . . . 206
  8.4 A Remote Calculator . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . 209
    8.4.1 JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
    8.4.2 Server Running on PC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
    8.4.3 Client Running on Android . . . . . . . .  . . . . . . . . . . . . . . . . . . 212
  8.5 Broadcast Receiver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
  8.6 Fetch Data From a Web Site . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
    8.6.1 Using a Broadcast Receiver . . . . . . . . . . . . . . . . . . . . . . . . . . 220
    8.6.2 Connecting to and Accessing a URL . . . . . .. . . . . . . . . . . . . . . . . 221
    8.6.3 UI Handled by MainActivity . . . . . . . . . . . . . . . . . . . . . . . . . . 225
  8.7 AIDL (Android Interface Definition Language) . . . . . . . . . . . . . . . . . . . 227
    8.7.1 AIDL Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
    8.7.2 The .aidl File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
    8.7.3 Implement AIDL Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . 229
    8.7.4 Expose AIDL Interface to Clients . . . . . . . . . . . . . . . . . . . . . . . 229
    8.7.5 A Remote Multiplier . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . 230

Chapter 9 Text To Speech (TTS) and Audio  237
  9.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
  9.2 The TextToSpeech Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
  9.3 A Simple TTS Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
  9.4 Writing Speech to a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
    9.4.1 Playback Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
    9.4.2 Saving Speech . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . 244
  9.5 Speak From a File with Pause/Resume . . .  . . . . . . . . . . . . . . . . . . . . 245
    9.5.1 Synchronization by Condition Variable . .. . . . . . . . . . . . . . . . . . . 245
    9.5.2 Speak Using a Different Thread . . . . . . . . . . . . . . . . . . . . . . . . 246
  9.6 Playing Audio Clips . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . 253
    9.6.1 Playing Sound . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . 253
    9.6.2 Simple Examples of Using MediaPlayer . . . . . . . . . . . . . . . . . . . . . 253
    9.6.3 Asynchronous Playback of MediaPlayer . . . . . . . . . . . . . . . . . . . . . 256

Chapter 10 Speech Recognition  263
  10.1 Introduction . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . 263
  10.2 A Simple Example . . . . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . 265

Chapter 11 Playing Video 269
  11.1 Introduction . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . 269
  11.2 A Simple Example . . . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . 269

Chapter 12 OpenCV For Android  273
  12.1 Introduction . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . 273
  12.2 OpenCV4Android SDK . . . . . . . . . . .  . . . . . . . . . . . . . . . . . . . . 273
    12.2.1 Getting the SDK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
    12.2.2 Import OpenCV Library and Samples . . . . . . . . . . . . . . . . . . . . . . 274
    12.2.3 Run OpenCV Samples . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . 275