About this ebook
Related to Asynchronous Android
Related ebooks
Mastering Android NDK Rating: 0 out of 5 stars0 ratingsAndroid for Beginners: Step by Step guide to develop Android App Rating: 0 out of 5 stars0 ratingsAndroid Native Development Kit Cookbook Rating: 0 out of 5 stars0 ratingsAndroid NDK: Beginner's Guide - Second Edition Rating: 0 out of 5 stars0 ratingsAndroid Studio Cookbook: Design, test, and debug your apps using Android Studio Rating: 4 out of 5 stars4/5Android Application Programming with OpenCV Rating: 3 out of 5 stars3/5Beginning Android Programming with Android Studio Rating: 0 out of 5 stars0 ratingsLearning Android Application Testing Rating: 0 out of 5 stars0 ratingsMastering Android Wear Application Development Rating: 0 out of 5 stars0 ratingsBeginning Android 4 Application Development Rating: 1 out of 5 stars1/5Corona SDK Mobile Game Development: Beginner's Guide - Second Edition Rating: 0 out of 5 stars0 ratingsAndroid Studio Development Essentials Rating: 5 out of 5 stars5/5Android App Development Tutorial . Build your first app. Rating: 0 out of 5 stars0 ratingsAndroid High Performance Programming Rating: 0 out of 5 stars0 ratingsAndroid Studio Electric Eel Essentials - Kotlin Edition: Developing Android Apps Using Android Studio 2022.1.1 and Kotlin Rating: 0 out of 5 stars0 ratingsAndroid Things Projects Rating: 0 out of 5 stars0 ratingsMobile Fix Rating: 0 out of 5 stars0 ratingsAndroid Programming For Beginners: The Simple Guide to Learning Android Programming Fast! Rating: 0 out of 5 stars0 ratingsMastering Visual Studio: A Comprehensive Guide Rating: 0 out of 5 stars0 ratingsTroubleshooting OpenVPN Rating: 0 out of 5 stars0 ratingsInstant Java Password and Authentication Security Rating: 0 out of 5 stars0 ratingsAndroid (operating system) A Clear and Concise Reference Rating: 0 out of 5 stars0 ratingsTesting and Securing Android Studio Applications Rating: 0 out of 5 stars0 ratingsMastering OpenCV 3 - Second Edition Rating: 0 out of 5 stars0 ratingsFlash Development for Android Cookbook Rating: 3 out of 5 stars3/5Exploring SE for Android Rating: 0 out of 5 stars0 ratingsLet's Use Bash on Windows 10! The Lite version Rating: 0 out of 5 stars0 ratingsHistory Of Google Android Rating: 0 out of 5 stars0 ratings
Internet & Web For You
The Digital Marketing Handbook: A Step-By-Step Guide to Creating Websites That Sell Rating: 5 out of 5 stars5/5Coding For Dummies Rating: 5 out of 5 stars5/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Cybersecurity For Dummies Rating: 5 out of 5 stars5/5Canva Tips and Tricks Beyond The Limits Rating: 3 out of 5 stars3/5How to Be Invisible: Protect Your Home, Your Children, Your Assets, and Your Life Rating: 4 out of 5 stars4/5UX/UI Design Playbook Rating: 4 out of 5 stars4/5The $1,000,000 Web Designer Guide: A Practical Guide for Wealth and Freedom as an Online Freelancer Rating: 4 out of 5 stars4/5The Beginner's Affiliate Marketing Blueprint Rating: 4 out of 5 stars4/5More Porn - Faster!: 50 Tips & Tools for Faster and More Efficient Porn Browsing Rating: 3 out of 5 stars3/5Notion for Beginners: Notion for Work, Play, and Productivity Rating: 4 out of 5 stars4/5The Logo Brainstorm Book: A Comprehensive Guide for Exploring Design Directions Rating: 4 out of 5 stars4/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/548 Really Useful Web Sites Rating: 5 out of 5 stars5/5Social Engineering: The Science of Human Hacking Rating: 3 out of 5 stars3/5Ready, Set, Brand!: The Canva for Work Quickstart Guide Rating: 5 out of 5 stars5/5The Gothic Novel Collection Rating: 5 out of 5 stars5/5Get Into UX: A foolproof guide to getting your first user experience job Rating: 4 out of 5 stars4/5Surveillance and Surveillance Detection: A CIA Insider's Guide Rating: 3 out of 5 stars3/5Publishing E-Books For Dummies Rating: 4 out of 5 stars4/5Stop Asking Questions: How to Lead High-Impact Interviews and Learn Anything from Anyone Rating: 5 out of 5 stars5/5Learn NodeJS in 1 Day: Complete Node JS Guide with Examples Rating: 3 out of 5 stars3/5CISM Certified Information Security Manager Study Guide Rating: 4 out of 5 stars4/5Web Design For Dummies Rating: 4 out of 5 stars4/5How To Start A Profitable Authority Blog In Under One Hour Rating: 5 out of 5 stars5/5Principles of Web Design Rating: 0 out of 5 stars0 ratings
Reviews for Asynchronous Android
1 rating0 reviews
Book preview
Asynchronous Android - Steve Liles
Table of Contents
Asynchronous Android
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers and more
Why Subscribe?
Free Access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Building Responsive Android Applications
Introducing the Dalvik Virtual Machine
Memory sharing and the Zygote
Understanding the Android thread model
The main thread
Unresponsive apps and the ANR dialog
Maintaining responsiveness
Concurrency in Android
Correctness issues in concurrent programs
Liveness issues in concurrent programs
Android-specific concurrency issues
The Activity lifecycle
Manipulating the user interface
Android-specific concurrency constructs
Summary
2. Staying Responsive with AsyncTask
Introducing AsyncTask
Declaring AsyncTask types
Executing AsyncTasks
Providing feedback to the user
Providing progress updates
Canceling AsyncTask
Handling exceptions
Controlling the level of concurrency
Common AsyncTask issues
Fragmentation issues
Activity lifecycle issues
Handling lifecycle issues with early cancellation
Handling lifecycle issues with retained headless fragments
Applications of AsyncTask
Summary
3. Distributing Work with Handler and HandlerThread
Understanding Looper
Building responsive apps with Handler
Scheduling work with post
Canceling a pending Runnable
Scheduling work with send
Canceling pending Messages
Composition versus Inheritance
Multithreaded example
Sending Messages versus posting Runnables
Building responsive apps with HandlerThread
Handler programming issues
Leaking implicit references
Leaking explicit references
Applications of Handler and HandlerThread
Summary
4. Asynchronous I/O with Loader
Introducing Loaders
Building responsive apps with AsyncTaskLoader
Building responsive apps with CursorLoader
Combining Loaders
Applications of Loaders
Summary
5. Queuing Work with IntentService
Introducing Service and IntentService
Building responsive apps with IntentService
Handling results
Returning results with PendingIntent
Posting results as system notifications
Applications of IntentService
HTTP uploads with IntentService
Reporting progress
Summary
6. Long-running Tasks with Service
Building responsive apps with Service
Controlling concurrency with Executors
Returning results with Messenger
Communicating with Services
Direct communication with local Services
Broadcasting results with Intents
Detecting unhandled broadcasts
Applications of Services
Summary
7. Scheduling Work with AlarmManager
Scheduling alarms with AlarmManager
Canceling alarms
Scheduling repeating alarms
Handling alarms
Handling alarms with Activities
Handling alarms with BroadcastReceiver
Doing work with BroadcastReceiver
Doing background work with goAsync
Handling alarms with Services
Staying awake with WakeLocks
Applications of AlarmManager
Summary
Index
Asynchronous Android
Asynchronous Android
Copyright © 2013 Packt Publishing
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.
First published: December 2013
Production Reference: 1171213
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78328-687-4
www.packtpub.com
Cover Image by Jarek Blaminsky (<[email protected]>)
Credits
Author
Steve Liles
Reviewers
David Bakin
Elie Abu Haydar
Hassan Makki
Matt Preston
Hélder Vasconcelos
Acquisition Editors
Harsha Bharwani
Kunal Parikh
Lead Technical Editor
Larissa Pinto
Technical Editors
Arwa Manasawala
Anand Singh
Proofreaders
Ameesha Green
Maria Gould
Copy Editors
Alisha Aranha
Mradula Hegde
Karuna Narayanan
Shambhavi Pai
Alfida Paiva
Lavina Pereira
Adithi Shetty
Project Coordinator
Jomin Varghese
Indexer
Hemangini Bari
Graphics
Yuvraj Mannari
Valentina D'silva
Production Coordinator
Manu Joseph
Cover Work
Manu Joseph
About the Author
Steve Liles is a self-confessed geek and has been an Android fan since the launch day of the G1. When he isn't at work building publishing systems and apps for newspapers and magazines, you'll find him tinkering with his own apps, building 3D printers, or playing RTS games. He is currently working with a start-up to build an advertising system that links the print and digital worlds using computer vision on Android and iOS devices.
I would like to sincerely thank the technical reviewers, who delivered their invaluable feedback gently and constructively. Without them, this book would be a pale shadow of the thing you now hold in your hands.
I once read that book acknowledgements are apologies to the people who have suffered. I must, therefore, unreservedly thank and apologize to my wife, not only for her patience and support during this project, but also through all the years and projects gone before.
About the Reviewers
David Bakin's first concurrent program was a Tektronix terminal emulator, written in assembly for an IMLAC PDS-1, which was a PDP-8-like machine with a GPU. He's written a lot of multiprocess and multithread concurrent programs and a number of tools for visualizing and debugging concurrent programs since then. He says computers now are orders of magnitude more powerful than the PDS-1, but concurrent programming hasn't gotten any easier.
His favorite languages are C++ and Haskell, and he prefers to use strong typing and functional programming techniques to write correct code. On the other hand, he also likes Smalltalk, Mathematica, and SIMD code in assembler because they're a lot of fun.
Elie Abu Haydar, born and raised in Beirut, has been interested in software development since his high school years. In 2006, he graduated from the American University of Science and Technology in Lebanon with a BS in Computer Science. As a software developer at KnowledgeView Ltd., Elie is seriously involved in every aspect of newsroom and publishing products where he uses Java to develop publishing solutions. These include backend web services and frontend web and mobile applications. Elie also contributes in the technical research and development of existing and future company projects.
Hassan Makki is a computer and communication engineer. He was born in 1979 in Lebanon and graduated in 2005. Since graduation, he has worked as a software engineer and has long-standing experience in C++, Java, and Android development. He began developing and managing Web and Android apps in 2011, and has developed around 30 apps related to news, music, sports, and advertisements.
Matt Preston is a professional software engineer with 14 years of experience in developing and maintaining a variety of systems for large international news and media organizations. He has worked on a range of low-latency/high-concurrency projects, ranging from mobile apps to distributed systems. Recently, he has been working on low-latency search and analytics using Elasticsearch.
I would like to thank my friend and colleague Steve Liles for this opportunity to let me review this book. We worked together for over 12 years and he's one of the best developers I have ever worked with. I learned a lot from him; I still continue to do so. Finally, thanks to Erica for all the tea; sorry about the late nights.
Hélder Vasconcelos has been a senior software engineer at Airtel ATN (Dublin, Ireland) since October 2012. He has extensive experience in designing and developing real-time/multithreaded Java and C/C++ applications for the telecommunications and aviation industry. Apart from his day-to-day job, for the last three years, he has been designing and developing native Android applications for Bearstouch Software and other third-party clients.
He graduated with a degree in Electronic & Telecommunications Engineering from the University of Aveiro in January 2006. He worked as a VoIP systems engineer at RedeRia Innovation (Aveiro, Portugal) from January 2006 to June 2007. He also worked as a software engineer at Outsoft/PT Inovação (Aveiro, Portugal) from October 2007 to October 2012.
Thanks to everyone involved in this project for your hard work and commitment, my awesome wife Tania for her love and support, and my parents and family for their awesome effort in my education. Additionally, I would like to thank my friends, colleagues, clients, and teachers for helping me to shape and improve my skills and perspectives during my career.
www.PacktPub.com
Support files, eBooks, discount offers and more
You might want to visit www.PacktPub.com for support files and downloads related to your book.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at
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.
Support files, eBooks, discount offers and morehttp://PacktLib.PacktPub.com
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across 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 web browser
Free Access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access.
Preface
Programming is the most fun a person can have on their own. This is a fact well-known to programmers, though it seems the rest of the world is yet to catch on. You already know this or you wouldn't be reading this book, but it constantly amazes me that more people aren't falling over themselves to learn to code.
Meanwhile, mobile devices have made computers fun even for non-coders. We carry in our pockets small machines with incredible processing power and a giddying array of sensors and interfaces.
Android takes these fun machines and makes them accessible to programmers through a fabulously well-crafted platform and tool chain, in a programming language that has stood the test of time yet continues to develop and evolve.
What could possibly be better than programming fun machines to do cool things, in a powerful language, on a well-crafted platform, with a world-class tool chain? For me, the answer is doing so with a good enough understanding of those things to make the difference between a good app and a great one.
There are many things that must come together to make a great app. You need a great idea—I can't help you there. You need a pretty user interface—sorry, wrong book. You need a great user experience—aha! Now we're getting somewhere. Among the many things that contribute to a great user experience, responsiveness is right up