Table Of ContentPractical
Android
14 Complete Projects on Advanced
Techniques and Approaches
—
Mark Wickham
Practical Android
14 Complete Projects on Advanced
Techniques and Approaches
Mark Wickham
Practical Android: 14 Complete Projects on Advanced Techniques and Approaches
Mark Wickham
Dallas, Texas, USA
ISBN-13 (pbk): 978-1-4842-3332-0 ISBN-13 (electronic): 978-1-4842-3333-7
https://doi.org/10.1007/978-1-4842-3333-7
Library of Congress Control Number: 2017964267
Copyright © 2018 by Mark Wickham
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
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.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the
material contained herein.
Cover image by Freepik (www.freepik.com)
Managing Director: Welmoed Spahr
Editorial Director: Todd Green
Acquisitions Editor: Steve Anglin
Development Editor: Matthew Moodie
Technical Reviewers: Jeff Friesen and Chaim Krause
Coordinating Editor: Mark Powers
Copy Editor: Mary Behr
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street,
6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected],
or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer
Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail [email protected], or visit www.apress.com/rights-
permissions.
Apress titles 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 Print and eBook Bulk Sales
web page at www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to readers
on GitHub via the book’s product page, located at www.apress.com/9781484233320. For more detailed
information, please visit www.apress.com/source-code.
Printed on acid-free paper
To my parents, who helped me discover computers and software when I was young.
Contents
About the Author ���������������������������������������������������������������������������������������������������xiii
About the Technical Reviewers �������������������������������������������������������������������������������xv
Acknowledgments �������������������������������������������������������������������������������������������������xvii
Preface �������������������������������������������������������������������������������������������������������������������xix
Introduction ������������������������������������������������������������������������������������������������������������xxi
■ Chapter 1: Introduction to JSON ����������������������������������������������������������������������������1
1.1 Introduction .............................................................................................................1
1.2 Chapter Projects ......................................................................................................2
1.3 JSON Overview .......................................................................................................2
1.4 JSON and Android ...................................................................................................3
1.5 Designing JSON ......................................................................................................4
JSON Sample File ...................................................................................................................................5
Validating JSON ......................................................................................................................................6
Text File Encoding ...................................................................................................................................7
1.6 Common JSON Operations ......................................................................................7
Setting a JSON Value in a JSON Object ..................................................................................................8
Getting a JSON Value from a JSON Object ..............................................................................................9
Creating a JSON File Programmatically ..................................................................................................9
v
vi Contents
Reading and Parsing a JSON File .........................................................................................................10
Printing JSON Strings ...........................................................................................................................11
Removing JSON Arrays .........................................................................................................................11
1.7 J SON Alternatives .................................................................................................13
G SON ....................................................................................................................................................13
Jackson ................................................................................................................................................14
1.8 R eferences ............................................................................................................15
A ndroid JSON .......................................................................................................................................15
Google GSON ........................................................................................................................................15
Third Party ............................................................................................................................................15
■ Chapter 2: Connectivity ���������������������������������������������������������������������������������������17
2.1 Introduction ...........................................................................................................17
2.2 Chapter Projects ....................................................................................................17
2.3 Connectivity Basics ...............................................................................................18
2.4 A ndroid HTTP Options............................................................................................19
2.5 Connectivity Status ...............................................................................................22
Using the Built in APIs...........................................................................................................................22
WiFi Broadcast Receiver .......................................................................................................................23
2.6 S erver Reachability ...............................................................................................24
Pinging with ICMP ................................................................................................................................25
Pinging with HTTP ................................................................................................................................26
HTTP Status Codes ...............................................................................................................................27
Blocked Protocols and URL Redirects...................................................................................................28
Generating 204 Responses ...................................................................................................................29
2.7 C onnections App ...................................................................................................29
C onnections App Overview ...................................................................................................................30
Connections Project ..............................................................................................................................31
Connections App Configuration ............................................................................................................31
MainActivity.java ...................................................................................................................................32
Interpreting Reachability Results..........................................................................................................36
Contents vii
2.8 S plash App ............................................................................................................37
S plash App Overview ............................................................................................................................38
Splash Project .......................................................................................................................................39
Splash.xml ............................................................................................................................................39
SplashActivity.java ................................................................................................................................41
2.9 E ssential Tools .......................................................................................................45
2.10 References ..........................................................................................................45
Android HTTP and Connectivity ............................................................................................................45
ApacheHttpClient ..................................................................................................................................46
OkHttp ...................................................................................................................................................46
Tools .....................................................................................................................................................46
■ Chapter 3: Lazy Loading Images �������������������������������������������������������������������������47
3.1 Introduction ...........................................................................................................47
3.2 Chapter Projects ....................................................................................................47
3.3 Lazy Loading Libraries ..........................................................................................48
3.4 Lazy Loading Architecture .....................................................................................49
M emory Cache ......................................................................................................................................50
Disk Cache ............................................................................................................................................51
3.5 Choosing a Library ................................................................................................52
G oogle Volley ........................................................................................................................................53
Square Picasso .....................................................................................................................................54
Facebook Fresco ..................................................................................................................................55
3.6 H andling Image Assets ..........................................................................................56
Size vs. Quality Tradeoff .......................................................................................................................57
Image Downsampling ...........................................................................................................................58
Aspect Ratio .........................................................................................................................................59
3.7 L azy Loading App ..................................................................................................60
Lazy Loading App Overview ..................................................................................................................61
Lazy Loading Project ............................................................................................................................62
XML Layout Files ..................................................................................................................................69
ImageLoader.java ..................................................................................................................................71
viii Contents
M emoryCache.java ...............................................................................................................................73
FileCache.java .......................................................................................................................................75
Adapters ...............................................................................................................................................76
Lazy List Adapter ..................................................................................................................................76
Lazy Grid Adapter .................................................................................................................................78
Lazy Gallery Adapter .............................................................................................................................81
3.8 R eferences ............................................................................................................83
Android and Java References ...............................................................................................................83
Third-Party Lazy Loading Libraries .......................................................................................................84
■ Chapter 4: Remote Crash Logs ����������������������������������������������������������������������������85
4.1 Introduction ...........................................................................................................85
4.2 Chapter Project .....................................................................................................85
4.3 Remote Crash Log Solutions .................................................................................86
How Crash Logging Works ....................................................................................................................88
4.4 A pp Crash Report for Android ................................................................................89
A CRA Overview .....................................................................................................................................89
4.5 A CRA Back-End Server ..........................................................................................91
4.6 Open Street Map for Android .................................................................................92
4.7 Remote Crash Log App ..........................................................................................95
Remote Crash Log App Overview .........................................................................................................95
Remote Crash Log Project ....................................................................................................................97
PHP Self-Hosted Script .......................................................................................................................101
4.8 R eferences ..........................................................................................................104
A ndroid ...............................................................................................................................................104
Google Firebase ..................................................................................................................................104
OSMdroid ............................................................................................................................................104
Third Party ..........................................................................................................................................104
■ Chapter 5: Uploading and Emailing �������������������������������������������������������������������105
5.1 Introduction .........................................................................................................105
5.2 Chapter Projects ..................................................................................................105
5.3 Overview .............................................................................................................106
Contents ix
5.4 Server Spinner: Server Setup ..............................................................................107
5.5 Server Spinner App .............................................................................................110
Server Spinner App Overview .............................................................................................................110
Server Spinner Project........................................................................................................................111
Deleting an Item .................................................................................................................................117
Utils.java .............................................................................................................................................118
Uploading an Item ...............................................................................................................................118
5.6 M obile Email .......................................................................................................120
5.7 Mobile Email Approaches ....................................................................................120
5.8 Email Protocols ...................................................................................................121
5.9 A ndroid Email Clients ..........................................................................................122
5.10 Emailing App: AWS Server Setup.......................................................................122
5.11 Emailing App .....................................................................................................124
A ndroid Intents ...................................................................................................................................124
JavaMail API .......................................................................................................................................125
External AWS Interface .......................................................................................................................125
Emailing App Overview .......................................................................................................................125
Emailing Project ..................................................................................................................................126
MainActivity.java .................................................................................................................................127
GMailSender.java ................................................................................................................................132
JSSEProvider.java ...............................................................................................................................133
5.12 R eferences ........................................................................................................133
U ploading Files ...................................................................................................................................133
Email ...................................................................................................................................................133
■ Chapter 6: Push Messaging �������������������������������������������������������������������������������135
6.1 Introduction .........................................................................................................135
6.2 Chapter Projects ..................................................................................................136
6.3 Push Messaging Overview ..................................................................................136
P ush Technologies ..............................................................................................................................137
How Push Messaging Works ..............................................................................................................139
Choosing a Technology .......................................................................................................................140
x Contents
6.4 Push Messaging Services ...................................................................................142
P ush Service Advantages ...................................................................................................................142
Choosing a Push Service ....................................................................................................................143
Services Setup Steps..........................................................................................................................144
6.5 Firebase Cloud Messaging ..................................................................................145
G CM/FCM Migration ............................................................................................................................145
FCM Setup ..........................................................................................................................................146
FCM Quickstart App ............................................................................................................................148
FCM App .............................................................................................................................................149
FCM Project ........................................................................................................................................150
Application Server Setup ....................................................................................................................154
FCM Upstream Messaging ..................................................................................................................159
6.6 Open Source Push Messaging with MQTT ..........................................................161
M QTT Introduction ..............................................................................................................................161
MQTT App ...........................................................................................................................................163
MQTT Project ......................................................................................................................................164
MQTT Message Brokers ......................................................................................................................169
MQTT Broker Setup for AWS ...............................................................................................................169
Sending Messages with MQTT Web Clients ........................................................................................170
MQTT Wrap Up ....................................................................................................................................171
6.7 R eferences ..........................................................................................................171
Firebase Cloud Messaging (FCM) .......................................................................................................171
MQTT Push Messaging .......................................................................................................................172
■ Chapter 7: Android Audio ����������������������������������������������������������������������������������173
7.1 Introduction .........................................................................................................173
7.2 Chapter Projects ..................................................................................................175
7.3 A udio Overview ...................................................................................................176
API and Class Summary .....................................................................................................................176
Choosing the Right API .......................................................................................................................178
Description:Choose the best approach for your app and implement your solution quickly by leveraging complete projects. This book is a collection of practical projects that use advanced Android techniques and approaches, written by Android instructor Mark Wickham. Mark has taught a series of popular classes at A