ebook img

Asynchronous Android Programming PDF

394 Pages·2016·2.304 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Asynchronous Android Programming

[ 1 ] Asynchronous Android Programming Second Edition Unlock the power of multi-core mobile devices to build responsive and reactive Android applications Helder Vasconcelos BIRMINGHAM - MUMBAI Asynchronous Android Programming Second Edition Copyright © 2016 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: July 2016 Production reference: 1260716 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78588-324-8 www.packtpub.com Credits Author Project Coordinator Helder Vasconcelos Nidhi Joshi Reviewer Proofreader Gavin Matthews Safis Editing Commissioning Editor Indexer Edward Gordon Mariammal Chettiyar Acquisition Editor Graphics Indrajit Das Disha Haria Content Development Editor Production Coordinator Siddhesh Salvi Arvindkumar Gupta Technical Editor Cover Work Danish Shaikh Arvindkumar Gupta Copy Editor Vibha Shukla About the Author Helder Vasconcelos is a Portuguese Software Engineer based on Dublin, Ireland, with more than 10 years of experience designing and developing real-time/multithreaded Java and C++ applications for the telecommunications and aviation industries. Apart from his day-to-day job, he occupies his spare time building native Android applications for Bearstouch Software and other third-party companies. He graduated with a degree in Electronic and Telecommunications Engineering from the University of Aveiro in January 2006. During his career, he has worked as a Software Engineer for companies such as PT Inovação (Portugal), Airtel ATN (Dublin, Ireland) and Axway (Dublin, Ireland). You can find Hélder on LinkedIn at (https://ie.linkedin.com/in/heldervasc/en) or on his website at (http://hvasconcelos.github.io). I would like to sincerely thanks all technical reviewers, but especially Gavin. I really appreciate your invaluable feedback and commit that shaped the quality of the book. A special thanks to my awesome wife Tania for encourage me when the lack of motivation was killing my productivity. It would not have been possible without your precious support. Thanks also to 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. About the Reviewer Gavin Matthews is a veteran software engineer specializing in enterprise scale B2B, MFT and EFSS systems. www.PacktPub.com eBooks, discount offers, and more 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 [email protected] for more details. 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 Chapter 2: Performing Work with Looper, Handler, and HandlerThread 31 Understanding Looper 32 Understanding Handler 34 Sending work to a Looper 37 Scheduling work with post 39 Using Handler to defer work 40 Leaking implicit references 41 Leaking explicit references 43 Updating the UI with Handler 45 Canceling a pending Runnable 46 Scheduling work with send 47 Cancelling pending messages 50 Composition versus inheritance 52 Multithreading with Handler and ThreadHandler 53 Looper message dispatching debugging 58 Sending messages versus posting runnables 59 Applications of Handler and HandlerThread 59 Summary 60 Chapter 3: Exploring the AsyncTask 61 Introducing AsyncTask 62 Declaring AsyncTask types 65 Executing AsyncTasks 67 Providing indeterministic progress feedback 69 Providing deterministic progress feedback 72 Canceling an AsyncTask 75 AsyncTask Execution State 78 Handling exceptions 79 Controlling the level of concurrency 81 Common AsyncTask issues 84 Fragmentation issues 84 Memory leaks 85 Activity lifecycle issues 85 Handling lifecycle issues with early cancellation 86 Handling lifecycle issues with retained headless fragments 87 Applications of AsyncTask 92 Summary 93 Chapter 4: Exploring the Loader 95 Introducing Loaders 96 Loader API 96 Loader 97 [ 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 ]

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.