Asynchronous Android Programming 2nd Edition Helder Vasconcelos pdf download
Asynchronous Android Programming 2nd Edition Helder Vasconcelos pdf download
https://ebookfinal.com/download/asynchronous-android-
programming-2nd-edition-helder-vasconcelos/
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
questions@packtpub.com, 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
A Few Typical Cases of Re-orders
Will you please take my order for twenty pounds of Hess Pecans from the
next crop? The nuts are just splendid, and we never tasted anything like them
before for flavor.
R. G. B.
Nov. 7, 1919.
Dec. 2, 1919.
The barrel of pecans arrived the day before Thanksgiving. The nuts are gone
and I am ready for more; wish the entire order before the Xmas holidays.
W. O. L.
Buying in 50 lb. F. B., Los Angeles, California (in the heart of the
lots finest walnut district), ordered 22 oz. box for $2,
Feb. 13th, 1917. March 11th, 1917, wrote: “They are
unquestionably the very best I ever ate, and I am wondering if you have more to
offer, and if so, the price in bulk.” Aug. 2, 1917, order booked for Fall, 1917,
delivery, 50 pounds Hess Brand Paper Shell Pecans.
Nov. 27, 1917, sent check for $50 in payment of 50 pounds.
February 26th, 1918, sent his third re-order for 50 lbs. of Hess Brand Paper
Shell Pecans for delivery, Fall, 1918, for $50.
In 1919, purchased 20 lbs., remitting $25.00; 1920, bought orchard units on
our plantations.
16 pounds in less Order received, Dec. 11, 1917, from Dr. M. B.,
than three Wabash, Ind., for $1 box of Hess Brand Paper Shell
months Pecans.
Jan. 8, 1918, “Enclosed find check for $5 for
which ship pecans like the 12 oz. box recently sent me. They are the finest I ever
ate.”
Jan. 24, 1918, sent check for $10 for more nuts.
Feb. 9, 1918, bought orchard units.
“Wish I had a J. C., Seattle, Washington, wrote Jan. 29, 1917:
barrel” “The size, quality, and flavor are all of the very
highest. They are richness itself. Regarding food
value, I question if there is any nut on earth equal to it. I wish I had a barrel of
them. You ought to plant at least 10,000 acres.”
April 10th, 1917, ordered 10 lbs. more for Fall, 1917, delivery, saying, “They
are the very best on earth.”
“The Finest Nuts I Ever Saw”
Says the world famous food authority, Dr. J. H.
Kellogg
Dr. J. H. Kellogg, head of the famous Battle Creek Sanitarium, is
a world famous expert on nuts. His writings, based on a half century
of research, have shown that pecan meat is suitable for “every
month in the year, for all climates, all work and all ages of
mankind (except infants)”, as Good Health stated. He has
directed attention to the fact that pecans give all the food elements
that animal flesh gives, in better proportion and with assured
freedom from impurity and disease. He has made clear the vital
importance of vitamines, found only to a very slight degree in animal
flesh, but profusely found in nuts.
His unquestioned leadership in this field gives added importance
to this letter:
Battle Creek Sanitarium
Battle Creek, Mich., Jan. 18, 1918.
Mr. Elam G. Hess, Pres.,
Keystone Pecan Co.,
Hess Pecans are the finest nuts I ever saw. What a blessing to the world it will
be when these fine products of the vegetable kingdom come to be better
appreciated by the public.
J. H. Kellogg.
From Another Food Authority
New York City, Dec. 27, 1916.
It is not strange that Hess Pecans are so much appreciated; they are so good
to eat. I ate a dozen at my supper and feel that could everyone eat them every
one would be benefited.
Dr. Elmer Lee (Formerly Editor Health Culture).
The Hess Brand Paper Shell Pecan is its own best advocate.
Those who taste it, quickly see why such superior pecans sell readily
at $1.25 per pound, while wild pecans are selling at 35c. per pound.
The only difficulty is that not one person in a thousand has ever
tasted the improved Paper Shell Pecan. Any thinking person,
checking over the records of increasing sales year after year, is sure
to agree with Burbank, America’s foremost horticulturist, when he
says, “We have now one pecan where we ought to have a
million.”
More Evidence of Superiority on Hess Brand
Paper Shell Pecans
Over 200 pounds The barrel of Hess Pecans that you sent me got
in a single order here in good condition, weight just as you say—202
lbs.—all right. They are certainly fine nuts and fine
to eat. Nuts and apples make a fine meal, take that from me. Friends of mine
think they are the best nuts (pecans) they ever came across. My advice to the
public—more nuts, less meats and there would be less sickness. I have lived on
nuts, fruit and vegetables for the last four years and never sick.
F. J. L.
Food value The box of Hess Pecans arrived O. K., but they
superior to beef didn’t last long. I never saw such a wonderful
product in my life, and as for food value—we need
not worry about Beef becoming short or extinct.
H. J. W., M.D.
They are the first pecans that I have ever seen and I must say that they come
entirely up to your description and are splendid nuts.
C. F. M.
I have received the box of Hess Pecans. I like them so well that I enclose
payment herewith, and request you to send a box to Mrs. G. D., New Orleans.
C. F. L.
Liked 10 lb. box, The 10 lb. Box of Pecans you sent me came to
orders two more hand and are good, fresh, and very fine. I enclose
you my check for $25 for two more 10 lb. boxes.
A. J.
By all odds the I received the box of Paper Shell Pecans, and
best enjoyed them immensely; would say they are by all
odds the best I have ever eaten. I have also eaten
the Creole Pralines of New Orleans, and the nuts used in that confection, although
good, do not compare.
W. S. M., Jr.
Quanah, Texas.
There are many reasons for this remarkable demand for the
finest grade pecans—despite the higher price—which reasons are
briefly indexed on the five following pages.
The superiority of The greatest of these reasons is the
these finer superior quality of these pecans, as shown
pecans by tests on pages 33 and 34—the fact that
they have a greater content of easily digested nut meat, of attractive
appearance and greatest nutritive value, which nut meat is easily
accessible, due to their thin shells.
The movement There is a strong movement the world
toward nut meat over toward nut meat as the “true meat,” in
as the “true which some have joined for religious
meat”
reasons, some for ethical reasons, others
from dietetic or hygienic considerations—and many others because
of increasing knowledge of food values.
The Seventh Day Adventists will refer you to the twenty-ninth
verse of the first chapter of Genesis, which reads:
“And God said, Behold, I have given you every herb bearing seed, which is
upon the face of all the earth, and every tree in the which is the fruit of a tree
yielding seed; to you it shall be for meat.” They reason that according to this
passage “true meat” grows on trees, and in this belief they are joined by many
others for ethical, dietetic and hygienic reasons.
By religious, Everywhere in America there are large
ethical and numbers of people, organized and
hygienic
unorganized, who will not eat the flesh of
organizations
any animal. In sanitaria of all sorts there is
a tendency to reduce to the minimum the use
of all animal meat or do away with it entirely.
In one system of forty sanitaria there are
practically no drugs used because the patients
are put on a perfected diet system in which
nuts are substituted for animal flesh. At Battle
Creek Sanitarium alone, under Dr. Kellogg,
over 10,000 patients have adopted the
meatless diet. Nut meat is largely used there
to replace animal flesh.
The nuts certainly are life size and look good enough to eat. Every one who
was so fortunate as to get some of the nuts, and they were quite a few,
pronounced them the finest ever. Here is wishing all good things for the pecan
company.
R. S.
More Pecan Orchards—A Vital Necessity
Our only problem NOW is to meet the demand for the highest
grade Paper Shell Pecans.
America demands more fine pecans—it is hungry for them.
“Once a pecan Not only because of the superior food
eater, always a value of pecans; nor only because of their
pecan eater” many advantages as the purest, most
condensed of all natural food products, but also because of their
alluring flavor. As Prof. Hutt, Ex-President of the American
Pomological Society, well puts it, “Once a pecan eater, always a
pecan eater.”
Wherever the improved pecan goes—the world over—it creates
its own market.
It is simply marvelous how hungry the world is for these fine
pecans, and it will be hungry for many years to come because the
increase in supply does not keep pace with the rapidly increasing
demand for high quality pecans. The obvious remedy, therefore, is to
produce more fine pecans by planting more pecan orchards.
Small branches showing
how pecan nuts grow.
How Pecan Trees Do Grow
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