Table Of ContentAndroid
Software
Development
Android
Software
Development
A Collection of
Practical Projects
Mark Wickham
Android Software Development
A collection of practical projects
by Mark Wickham
Copyright © 2017 by Mark Wickham
All rights reserved.
Editor: Mary Sue Wickham
Cover design: Mark Wickham
While every attempt has been taken in the preparation of this book, the
publisher and author assume no responsibility for errors or omissions,
or for damages resulting from the use of the information contained
herein nor the companion software provided.
ISBN: 9781530149025
Printing History:
First Print January 2017
Printed in the United States of America
To my parents
who helped me discover
computers and software
when I was young.
P
Preface
Overview
Androidd Software Development is a new book by Android instructor
Mark Wickham. Mark has been teaching a series of popular classes at
Android development conferences since 2013. This new book covers
content from his most popular classes. Each chapter covers an important
concept and provides the reader with a deep dive into the
implementation.
The book uses a project-based approach. Unlike most books on the
market today, Android Software Development provides complete working
code for all of the projects. Developers appreciate this approach because
it enables them to focus on their apps, and not waste time trying to
integrate code snippets or troubleshoot environment setup issues.
Android Software Development is an ideal resource for developers who
have some development experience, but may not be Android or mobile
development experts. Each chapter includes at least one complete
project to show the reader how to implement the concepts. In Android,
there are always multiple ways to accomplish a given task. Android
Software Development helps developers choose the best approach for their
app and lets developers implement their solution quickly by leveraging
complete projects.
Preface | 7
Audience
This book is intended for software developers who want to create apps
for the Android platform.
Because we are developing for Android, in addition to mastering the Java
programming language, we must also master the intricacies of the
platform itself.
s not easy to create commercial apps for the Android platform.
Developers can easily become frustrated for a variety reasons:
(cid:2) Project setup is not trivial in Android. Whether you are using the
latest version of Android Studio or an alternative development
environment such as Eclipse, it can sometimes be frustrating
making sure all of the libraries, manifest, and project assets are set
up correctly.
(cid:2) When developers identify the requirement for a specific
functionality in their Android App, it is often difficult to find
working code or to integrate the code snippets which are readily
available from various online resources.
(cid:2) The Android platform and Java programming language have many
functionality. There are often multiple approaches to implement a
particular desired feature. The book helps developers decide the
best approach for their apps.
(cid:2) We often need to make architecture decisions when writing
Android apps, especially when we require a server connection. It
takes experience to implement solutions which can meet
performanc
right architecture, developers can be faced with a code rewrite or
refactoring effort after the app is launched.
(cid:2) There are a lot of code fragments available online. However, it is
often not easy to integrate these assets into your own project. They
may be incomplete or frequently contain errors and are thus not
able to be compiled. The book contains 14 compete Android
projects. It is easy to get the projects up and running.
Preface | 8
Table of Contents
P Preface ............................................................................................. 7
Overview ............................................................................................ 7
Audience ............................................................................................. 8
Table of Contents ............................................................................... 9
Chapter Layout ................................................................................ 14
Project Summary .............................................................................. 15
Conventions ..................................................................................... 20
Companion Website ........................................................................ 21
Development Environment ............................................................. 22
Software License............................................................................... 22
Acknowledgements .......................................................................... 23
About the Author ............................................................................. 23
Colophon.......................................................................................... 24
1 Introduction to JSON .................................................................... 25
1.1 Introduction ............................................................................... 25
1.2 Chapter Projects ......................................................................... 26
1.3 JSON Overview ........................................................................... 26
1.4 JSON and Android ...................................................................... 28
1.5 Designing JSON .......................................................................... 28
JSON Sample File ...................................................................... 29
Validating JSON ....................................................................... 30
Text File Encoding .................................................................... 31
1.6 Common JSON Operations ........................................................ 32
Setting a JSON Value in a JSON Object .................................... 32
Getting a JSON Value from a JSON Object .............................. 33
Creating a JSON file Programmatically .................................... 34
Reading and Parsing a JSON file............................................... 35
Printing JSON Strings ............................................................... 35
Removing JSON Arrays ............................................................. 36
1.6 JSON Alternatives ....................................................................... 37
GSON ........................................................................................ 38
Jackson ..................................................................................... 39
1.7 References ................................................................................... 40
Android JSON ........................................................................... 40
Google GSON ........................................................................... 40
3rd Party ................................................................................... 40
2 Connectivity .................................................................................. 41
2.1 Introduction ............................................................................... 41
2.2 Chapter Projects ......................................................................... 42
2.3 Connectivity Basics .................................................................... 42
2.4 Android HTTP Options .............................................................. 44
2.5 Connectivity Status .................................................................... 47
Preface | 9
Using the Built in APIs ............................................................. 47
Wifi Broadcast Receiver ............................................................ 48
2.6 Server Reachability .................................................................... 49
Pinging with ICMP ................................................................... 49
Pinging with HTTP ................................................................... 51
2.7 Connections App ........................................................................ 55
Connections App Overview ..................................................... 55
Connections Project ................................................................. 56
2.8 Splash App .................................................................................. 63
Splash App Overview ............................................................... 63
Splash Project ........................................................................... 65
2.9 Essential Tools ............................................................................ 70
2.10 References ................................................................................. 72
Android HTTP and Connectivity ............................................. 72
ApacheHttpClient .................................................................... 72
OkHttp ...................................................................................... 72
Tools ......................................................................................... 72
3 Lazy Loading Images .................................................................... 73
3.1 Introduction ............................................................................... 73
3.2 Chapter Projects ......................................................................... 74
3.3 Lazy Loading Libraries ............................................................... 74
3.4 Lazy Loading Architecture ......................................................... 75
Memory Cache ......................................................................... 77
Disk Cache ................................................................................ 78
3.5 Choosing a Library ..................................................................... 79
Google Volley ........................................................................... 80
Square Picasso ........................................................................... 81
Facebook Fresco ........................................................................ 82
3.6 Handling Image Assets ............................................................... 83
Size Verse Quality Tradeoff ...................................................... 84
Image Down Sampling ............................................................. 85
Aspect Ratio .............................................................................. 86
3.7 Lazy Loading App ...................................................................... 87
Lazy Loading App Overview .................................................... 88
Lazy Loading Project ................................................................ 89
3.8 References ................................................................................. 110
Android & Java References ..................................................... 110
3rd Party Lazy Loading Libraries ............................................ 110
4 Remote Crash Logs ...................................................................... 113
4.1 Introduction ............................................................................. 113
4.2 Chapter Projects ....................................................................... 114
4.3 Remote Crash Log Solutions .................................................... 114
How Crash Logging Works .................................................... 116
4.4 App Crash Report for Android - ACRA .................................... 117
ACRA Overview ...................................................................... 118
Preface | 10
4.5 ACRA Back End Server ............................................................. 119
4.6 Open Street Map for Android .................................................. 121
4.7 Remote Crash Log App ............................................................ 124
Remote Crash Log App Overview .......................................... 124
Remote Crash Log Project ...................................................... 127
4.8 References ................................................................................. 133
Android .................................................................................. 133
Google Firebase ...................................................................... 133
OSMdroid ............................................................................... 133
3rd Party ................................................................................. 133
5 Uploading & Emailing ................................................................ 135
5.1 Introduction ............................................................................. 135
5.2 Chapter Projects ....................................................................... 136
5.3 Overview .................................................................................. 136
5.4 Server Spinner - Server Setup ................................................... 137
5.5 Server Spinner App................................................................... 140
Server Spinner App Overview ................................................. 140
Server Spinner Project ............................................................ 141
5.6 Mobile Email ............................................................................ 150
5.7 Mobile Email Approaches ........................................................ 151
5.8 Email Protocols ........................................................................ 152
5.9 Android Email Clients ............................................................. 153
5.10 Emailing App - Server Setup .................................................. 153
5.11 Emailing App ......................................................................... 154
Emailing App Overview ......................................................... 155
Emailing Project ..................................................................... 157
5.12 References ............................................................................... 165
Uploading ............................................................................... 165
Email ....................................................................................... 165
6 Push Messaging ........................................................................... 167
6.1 Introduction ............................................................................. 167
6.2 Chapter Projects ....................................................................... 168
6.3 Push Messaging Overview ........................................................ 168
Origins of Push Messaging ..................................................... 169
Push Technologies .................................................................. 170
How Push Messaging Works .................................................. 172
Choosing a Technology ......................................................... 173
Firebase Cloud Messaging ...................................................... 175
FCM Quickstart App ............................................................... 177
FCM App Server ...................................................................... 178
GCM/FCM Considerations ..................................................... 179
6.4 Push Messaging Services .......................................................... 179
Advantages ............................................................................. 180
Choosing a Push Service ........................................................ 180
Setup Steps .............................................................................. 181
Preface | 11
6.5 Google Cloud Messaging .......................................................... 182
GCM Setup ............................................................................. 182
GCM App................................................................................ 183
GCM Project ........................................................................... 184
GCM Server Setup .................................................................. 188
GCM Upstream Messaging ..................................................... 191
GCM Topic Messaging ........................................................... 192
GCM/FCM Wrap up ............................................................ 193
6.6 Open Source Push Messaging with MQTT............................... 194
MQTT Introduction ................................................................ 194
MQTT App .............................................................................. 196
MQTT Project ......................................................................... 197
MQTT Server Setup Message Broker .................................... 202
Sending Messages MQTT Web Clients ................................ 202
6.7 References ................................................................................. 205
Push Services .......................................................................... 205
Google Cloud Messaging (GCM) ........................................... 205
Firebase Cloud Messaging (FCM) ........................................... 205
MQTT Push Messaging ........................................................... 206
7 Android Audio............................................................................. 207
7.1 Introduction ............................................................................. 207
7.2 Chapter Projects ....................................................................... 210
7.3 Audio Overview ........................................................................ 211
API and Class Summary ......................................................... 211
Choosing the Right API .......................................................... 213
7.4 Latency ..................................................................................... 214
Latency Breakdown ................................................................ 216
Audio Buffer Size App ............................................................. 218
Audio Buffer Size Project ........................................................ 220
7.5 Playing Audio ........................................................................... 222
Playing Audio App ................................................................. 223
Playing Audio Project ............................................................. 224
7.6 Playing Audio with a Background Service .............................. 229
Music Service App .................................................................. 229
Music Service Project .............................................................. 231
7.7 Recording Audio ...................................................................... 233
Recording Audio App ............................................................. 233
Recording Audio Project ......................................................... 234
7.8 Advanced Audio ....................................................................... 240
Built-in Audio Effects ............................................................. 240
Encoding ................................................................................ 242
7.9 Audio Synthesis ........................................................................ 245
Puredata Overview ................................................................. 247
Puredata Player App ............................................................... 249
Puredata Player Project ........................................................... 251
Preface | 12