Whether you are Android beginner developer or an Android seasoned programmer, this book will explore how to achieve efficient and reliable multithreaded Android applications.
We'll look at best asynchronous constructs and techniques, commonly used by Android Developer community, to execute computation intensive or blocking tasks off the main thread, keeping the UI responsive, telling the user how things are going, making sure we finish what we started, using those powerful multicore processors, and doing it all without wasting the battery.
By using the right asynchronous construct, much of the complexity is abstracted from the developer, making the application source code more readable and maintainable and less error prone.
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.
Chapter 1, Asynchronous Programming in Android, gives an overview of the Android process and thread model, and describes some of the challenges and benefits of concurrency in general, before discussing issues specific to Android.
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 6, Scheduling Work with AlarmManager, introduces to us a system API that could be used to defer work or create periodic tasks. The scheduled task could wake up the device to complete the work or alert users to new content.
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 8, Interacting with the Network, we cover in detail HttpUrlConnection
Android HTTP client. With the HttpUrlConnection
HTTP client, we will create an asynchronous toolkit that is able to fetch JSON documents, XML or text from a remote server.
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.
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.
To follow along and experiment with the examples, you will need a development computer with a Java 7 (or 8) SE Development Kit and the Android Software Development Kit Version 9 or above (you will need at least Version 21 to try all of the examples).
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.
This book is for Android Developers who want to learn how to build multithreaded and reliable Android applications using high level and advanced asynchronous techniques and concepts.
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.
No prior knowledge of of concurrent and asynchronous programming is required. This book is also targeted towards Java experts who are new to Android.
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.
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."
A block of code is set as follows:
[default] exten => s,1,Dial(Zap/1|30) exten => s,2,Voicemail(u100) exten => s,102,Voicemail(b100) exten => i,1,Voicemail(s0)
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)
Any command-line input or output is written as follows:
# cp /usr/src/asterisk-addons/configs/cdr_mysql.conf.sample /etc/asterisk/cdr_mysql.conf
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."
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.
To send us general feedback, simply e-mail <[email protected]>
, and mention the book's title in the subject of your message.
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.
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.
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
Log in or register to our website using your e-mail address and password.
Hover the mouse pointer on the SUPPORT tab at the top.
Click on Code Downloads & Errata.
Enter the name of the book in the Search box.
Select the book for which you're looking to download the code files.
Choose from the drop-down menu where you purchased this book from.
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:
WinRAR / 7-Zip for Windows
Zipeg / iZip / UnRarX for Mac
7-Zip / PeaZip for Linux
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!
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.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
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.
Please contact us at <[email protected]>
with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
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.