Asynchronous Android Programming 2nd Edition Helder Vasconcelos - Download the ebook now and read anytime, anywhere
Asynchronous Android Programming 2nd Edition Helder Vasconcelos - Download the ebook now and read anytime, anywhere
https://ebookfinal.com/download/android-programming-tutorials-3rd-
edition-mark-l-murphy/
https://ebookfinal.com/download/professional-android-sensor-
programming-1st-edition-greg-milette/
https://ebookfinal.com/download/reactive-programming-with-rxjs-
untangle-your-asynchronous-javascript-code-1st-edition-sergi-mansilla/
https://ebookfinal.com/download/the-android-developer-s-cookbook-2nd-
edition-building-applications-with-the-android-sdk-ronan-schwarz/
Android Application Development For Dummies 2nd Edition
Michael Burton
https://ebookfinal.com/download/android-application-development-for-
dummies-2nd-edition-michael-burton/
https://ebookfinal.com/download/handbook-of-asynchronous-machines-
with-variable-speed-1st-edition-hubert-razik/
https://ebookfinal.com/download/a-designer-s-guide-to-asynchronous-
vlsi-1st-edition-peter-a-beerel/
https://ebookfinal.com/download/programming-in-haskell-2nd-edition-
graham-hutton/
https://ebookfinal.com/download/programming-in-lua-2nd-edition-
roberto-ierusalimschy/
Asynchronous Android Programming 2nd Edition Helder
Vasconcelos Digital Instant Download
Author(s): Helder Vasconcelos
ISBN(s): 9781785883248, 1785883240
Edition: 2nd
File Details: PDF, 2.30 MB
Year: 2016
Language: english
[1]
Asynchronous Android
Programming
Second Edition
Helder Vasconcelos
BIRMINGHAM - MUMBAI
Asynchronous Android Programming
Second Edition
All rights reserved. No part of this book may be reproduced, stored in a retrieval
system, or transmitted in any form or by any means, without the prior written
permission of the publisher, except in the case of brief quotations embedded in
critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy
of the information presented. However, the information contained in this book is
sold without warranty, either express or implied. Neither the author, nor Packt
Publishing, and its dealers and distributors will be held liable for any damages
caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the
companies and products mentioned in this book by the appropriate use of capitals.
However, Packt Publishing cannot guarantee the accuracy of this information.
ISBN 978-1-78588-324-8
www.packtpub.com
Credits
Reviewer Proofreader
Gavin Matthews Safis Editing
Copy Editor
Vibha Shukla
About the Author
At www.PacktPub.com, you can also read a collection of free technical articles, sign
up for a range of free newsletters and receive exclusive discounts and offers on Packt
books and eBooks.
TM
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital
book library. Here, you can search, access, and read Packt's entire library of books.
Why subscribe?
• Fully searchable across every book published by Packt
• Copy and paste, print, and bookmark content
• On demand and accessible via a web browser
Table of Contents
Preface vii
Chapter 1: Asynchronous Programming in Android 1
Android software stack 2
Dalvik runtime 2
ART runtime 3
Memory sharing and Zygote 4
Android process model 5
Process ranks 5
Process sandboxing 6
Android thread model 6
The main thread 7
The Application Not Responding (ANR) dialog 9
Maintaining responsiveness 10
Concurrency in Android 12
Correctness issues in concurrent programs 14
Liveness issues in concurrent programs 16
Thread coordination 16
Concurrent package constructs 19
Executor framework 20
Android primary building blocks 21
Activity concurrent issues 21
Manipulating the user interface 23
Service concurrent issues 25
Started services issues 25
Bound services issues 26
Service in a separate process 27
Broadcast receiver concurrent issues 28
Android concurrency constructs 28
Summary 29
[i]
Table of Contents
[ ii ]
Table of Contents
Loader Manager 98
LoaderManager.LoaderCallbacks 99
Loader lifecycle 100
Loading data with Loader 101
Building responsive apps with AsyncTaskLoader 107
Building responsive apps with CursorLoader 116
Combining Loaders 119
Applications of Loader 125
Summary 125
Chapter 5: Interacting with Services 127
Introducing Service 128
Started service 131
Building responsive apps with IntentService 136
Handling results 139
Posting results with PendingIntent 139
Posting results as system notifications 142
Applications of IntentService 143
HTTP uploads with IntentService 144
Reporting progress 146
Bound Service 149
Communicating with a Local Service 150
Broadcasting results with intents 158
Detecting unhandled broadcasts 160
Applications of Services 163
Summary 164
Chapter 6: Scheduling Work with AlarmManager 165
Introducing AlarmManager 166
Scheduling alarms with AlarmManager 167
Setting alarms in recent Android versions 169
Testing your alarms in Doze Mode 172
Setting a Window alarm 173
Debugging AlarmManager alarms 173
Canceling alarms 175
Scheduling repeating alarms 176
Scheduling an alarm clock 178
Handling alarms 179
Handling alarms with Activities 180
Handling alarms with BroadcastReceiver 181
Working with BroadcastReceiver 183
Asynchronous work with goAsync 186
[ iii ]
Table of Contents
[ iv ]
Table of Contents
[v]
Table of Contents
[ vi ]
Preface
Whether you are Android beginner developer or an Android seasoned programmer,
this book will explore how to achieve efficient and reliable multithreaded Android
applications.
Using step-by-step guidelines and code examples, you will learn how manage
interactions between several threads and avoid concurrency and synchronization
problems that might occur when two or more threads access a shared resource to
complete a background job, to update the UI or retrieve the latest application data.
At the end of this journey you will know how build well-behaved applications with
smooth, responsive user-interfaces that delight users with speedy results and data
that's always fresh.
[ vii ]
Preface
Chapter 2, Performing Work with Looper, Handler and HandlerThread details the
fundamental and related topics of Handler, HandlerThread, and Looper, and
illustrates how they can be used to schedule tasks on the main thread, and to
coordinate and communicate work between cooperating background threads.
Chapter 3, Exploring the AsyncTask, covers the most common concurrent construct of
programming in Android. We learn how AsyncTask works, how to use it correctly,
and how to avoid the common pitfalls that catch out even experienced developers.
Chapter 4, Exploring the Loader, introduces the Loader framework and tackles the
important task of loading data asynchronously to keep the user interface responsive
and glitch free.
Chapter 5, Interacting with Services, we explored the very powerful Service Android
component, putting it to use to execute long-running background tasks with or
without a configurable level of concurrency. This component gives us the means to
perform background operations beyond the scope of a single Activity lifecycle and
to ensure that our work is completed even if the user leaves the application.
Chapter 7, Exploring the JobScheduler API, covers a job scheduling system API
introduced with Android Lollipop that allows us to start background work
when a set of device conditions, such as energy or network, are fulfilled.
Chapter 9, Asynchronous Work on the Native layer, introduces the JNI interface, an
Java standard interface that will allow us to execute concurrent tasks on native
code (C/C++), interact with the Java code from the native layer or update the
UI from the native code.
Chapter 10, Network Interactions with GCM, we will learn how to use the Google GCM
to efficiently push and pull efficiently realtime messages from your server and how
to schedule work with Google Play Services framework.
Chapter 11, Exploring Bus-based Communications, we will introduce to the reader the
publish-subscribe messaging pattern and the Event Bus Library, a publish-subscribe
implementation that allow us to deliver asynchronous messages between Android
application components.
[ viii ]
Preface
Chapter 12, Asynchronous Programing with RxJava, we will introduce RxJava, a library
used to easily compose asynchronous and event-based tasks on Java by using
observable data streams.
You will also need Android Studio IDE. The examples have been developed using
Google's new Android Studio IDE and use its integrated build system, Gradle.
While you can run the examples using the emulator provided by the Android SDK, it
is a poor substitute for the real thing. A physical Android device is a much faster and
more pleasurable way to develop and test Android applications!
Many of the examples will work on a device running any version of Android since
2.3, GingerBread. Some examples demonstrate newer APIs and as a result, require a
more recent Android version—up to Android 5, Lollipop.
They want to learn this technology because they want learn how to build efficient
applications that are able to interact orderly with internal/external services and
frameworks using Android standard constructs and APIs.
Conventions
In this book, you will find a number of text styles that distinguish between different
kinds of information. Here are some examples of these styles and an explanation of
their meaning.
[ ix ]
Preface
Code words in text, database table names, folder names, filenames, file extensions,
pathnames, dummy URLs, user input, and Twitter handles are shown as follows:
"We can include other contexts through the use of the include directive."
When we wish to draw your attention to a particular part of a code block, the
relevant lines or items are set in bold:
[default]
exten => s,1,Dial(Zap/1|30)
exten => s,2,Voicemail(u100)
exten => s,102,Voicemail(b100)
exten => i,1,Voicemail(s0)
New terms and important words are shown in bold. Words that you see on the
screen, for example, in menus or dialog boxes, appear in the text like this: "Clicking
the Next button moves you to the next screen."
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about
this book—what you liked or disliked. Reader feedback is important for us as it helps
us develop titles that you will really get the most out of.
[x]
Preface
If there is a topic that you have expertise in and you are interested in either writing
or contributing to a book, see our author guide at www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to
help you to get the most from your purchase.
1. Log in or register to our website using your e-mail address and password.
2. Hover the mouse pointer on the SUPPORT tab at the top.
3. Click on Code Downloads & Errata.
4. Enter the name of the book in the Search box.
5. Select the book for which you're looking to download the code files.
6. Choose from the drop-down menu where you purchased this book from.
7. Click on Code Download.
You can also download the code files by clicking on the Code Files button on the
book's webpage at the Packt Publishing website. This page can be accessed by
entering the book's name in the Search box. Please note that you need to be logged in
to your Packt account.
Once the file is downloaded, please make sure that you unzip or extract the folder
using the latest version of:
[ xi ]
Preface
The code bundle for the book is also hosted on GitHub at https://github.com/
PacktPublishing/Asynchronous-Android-Programming. We also have other code
bundles from our rich catalog of books and videos available at https://github.
com/PacktPublishing/. Check them out!
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes
do happen. If you find a mistake in one of our books—maybe a mistake in the text or
the code—we would be grateful if you could report this to us. By doing so, you can
save other readers from frustration and help us improve subsequent versions of this
book. If you find any errata, please report them by visiting http://www.packtpub.
com/submit-errata, selecting your book, clicking on the Errata Submission Form
link, and entering the details of your errata. Once your errata are verified, your
submission will be accepted and the errata will be uploaded to our website or added
to any list of existing errata under the Errata section of that title.
Piracy
Piracy of copyrighted material on the Internet is an ongoing problem across all
media. At Packt, we take the protection of our copyright and licenses very seriously.
If you come across any illegal copies of our works in any form on the Internet, please
provide us with the location address or website name immediately so that we can
pursue a remedy.
We appreciate your help in protecting our authors and our ability to bring you
valuable content.
Questions
If you have a problem with any aspect of this book, you can contact us at
[email protected], and we will do our best to address the problem.
[ xii ]
Asynchronous Programming
in Android
Asynchronous programming has become an important topic of discussion in the past
few years, especially when using the concurrent processing capabilities available on
the most recent mobile hardware.
Android applications, since they mostly run on devices with multiple units of
processing, should take advantage of asynchronous programming to scale and
improve the application performance when blocking operations, and when
CPU-intensive tasks are required.
Android is an open source operating system (OS) based on Linux kernel that was
devised in 2003 by Andy Rubin, Nick Sears, Chris White, and Rick Miner, and then
acquired by Google in July, 2005.
The Android OS, actually maintained by Google and the Open Handset Alliance,
was created to provide an open mobile-device platform for devices with limited
resources of computation, memory, and energy.
The platform has been incorporating advanced mobile devices standards, such as
NFC and Bluetooth LE, and its scope has grown from a pure smartphone platform to
a broader software platform for smart watches, TVs, tablets, and consoles.
[1]
Discovering Diverse Content Through
Random Scribd Documents
Aveling's town, King, 59.
Ireby, 99.
Irt, 59.
Keats, 187.
Kendal, 51, 53, 87, 118, 121,
122, 132; Grammar School,
88.
Ketel's Well, 183.
Kettle, Roman, 100.
Keswick, 52, 73, 100, 108, 112,
151, 156, 157, 166, 176, 183,
208; 224; Sea-gulls in, 64.
Kiln Hill, 201.
Kirkstone Pass, 119.
Krall of a Norse Chieftain, 99.
Orthwaite, 97.
Page-boy's reminiscences of
Wordsworth, 44.
Patterdale, 167.
Patrick, James, of Kendal, 122.
Paris Exhibition, 90.
Pearson, Mrs., 118.
Pearson, William, of Borderside,
116; his admiration for
Wordsworth, 125; his
character, 128; letter to
Wordsworth, 136; his tour on the
Continent, 138 f.
Peat-fuel, 234.
Peel's horn, 101; his Norse
ancestor, 107.
Peel, John, 196, 201.
Pelter Bridge, 41.
Penistone, 219.
Penrith, 52, 218, 220, 221, 223.
Play, At the Grasmere, 69;
analysis of, 74 f.
Poems, by W. Wordsworth, 125.
Poole, 224.
Portinscale, 197.
Prelude, The, by Wordsworth,
121, 122.
Protestant Alliance, 136.
Rake, 97.
Ravenglass, 60, 68, 183; sand-dunes, 59.
Red Pike, 172.
Redbank, 27, 35, 48.
Reed's English Literature, 141.
Richardson, John, 200.
Robinson, 216.
Robinson Crusoe, 120.
Roman Camp, 105.
Roncesvalles, 120.
Rossetti, Christina, 92.
Ruskin, 2, 211.
Rydal chimneys, 19 f., 31.
Rydal Falls, 46.
Rydal Valley, 17.
Saddleback, 152.
St. John-in-the Vale, 200, 208.
Scafell, 102, 109, 228.
Scott, Sir Walter, 2, 141.
Sea-gulls, 60 f.
Seascale, 63.
Sedbergh Grammar School, 88.
Shakespeare, 85, 128, 141, 221.
Sheep, Skiddaw, 231.
Shepherd's Crag, 178.
Shepherd's Cross, 232.
Shepherd Lord, Song of the, by
Wordsworth, 191.
Shoulthwaite, 193.
Silence, Mountain, 227.
Skiddaw, 94, 96, 98, 100, 101,
109, 113, 157, 168, 178, 188,
194, 227.
Smith, Thomas, 127, 134.
Solway, 101.
Somersby Wold, 1.
Southey, 50, 94, 107, 181; letter
to his brother, 187.
Spedding, James, 96.
Spenser, 221.
Squirrels, 210.
Stanley, A Shepherd's likeness
to Dean, 151.
Stockdale, 151.
Stott of Oldham, 61.
Strathclyde, 112.
Styhead Pass, 183, 197.
Superstitions of Westmoreland,
146.
Swallows, 229.
Swan, Inn, 36, 39.
Tarn-foot, 48.
Tebay, 143.
Tennyson, 96, 227.
Tewfit tarn, 208.
Theatre, Grasmere, 73.
Threlkeld, 190, 192, 203, 204.
Tilberthwaite's Yew-tree, 167.
Titmice, 213.
Tongue Ghyll beck, 31.
Town End, 20, 37.
Y
Yews, The, 118, 122, 124, 131,
132.
Yew-tree, A famous, 166.
Yorkshire, 129.
*** END OF THE PROJECT GUTENBERG EBOOK LAKE
COUNTRY SKETCHES ***
Updated editions will replace the previous one—the old editions will
be renamed.
1.D. The copyright laws of the place where you are located also
govern what you can do with this work. Copyright laws in most
countries are in a constant state of change. If you are outside the
United States, check the laws of your country in addition to the terms
of this agreement before downloading, copying, displaying,
performing, distributing or creating derivative works based on this
work or any other Project Gutenberg™ work. The Foundation makes
no representations concerning the copyright status of any work in
any country other than the United States.
• You pay a royalty fee of 20% of the gross profits you derive from
the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”
• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.
1.F.
Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.
ebookfinal.com