SlideShare a Scribd company logo
Android
Thread, Handler and AsyncTask
                        Hoang Ngo
Thread
   • Act much like usual Java Threads

   • Can’t act directly on external User
     Interface objects (throw the Exception
     CalledFromWrongThreadException: Only
     the original thread that created a view
     hierarchy can touch its views”)
   • Can’t be stopped by executing destroy()
     nor stop(). Use instead interrupt() or join()
     (by case)
Thread
   • Two main ways of having a Thread
     execute application code:
     – Providing a new class that extends Thread and
       overriding its run() method.
     – Providing a new Thread instance with a
       Runnable object during its creation. In both
       cases, the start() method must be called to
       actually execute the new Thread.
Thread example
Handler

   • Associated with a single thread and
     that thread's message queue
   • Bound to the thread / message
     queue of the thread that is creating it
   • Deliver messages and runnables to
     that message queue
   • Execute them as they come out of
     the message queue
Handler

   • Two main uses for a Handler:
     – To schedule messages and
       runnables to be executed as some
       point in the future
     – To add an action into a queue
       performed on a different thread
Handler example
AsyncTask
    • Created on the UI thread and can be executed
      only once

    • Run on a background thread and result is
      published on the UI thread

    • The three types used by an asynchronous task
      are the following
       – Params, the type of the parameters sent to the task
         upon execution
       – Progress, the type of the progress units published during
         the background computation
       – Result, the type of the result of the background
         computation

    • Extend AsyncTask<Void, Void, Void>
AsyncTask
   • Go through 4 steps:
     – onPreExecute(): invoked on the UI thread
       immediately after the task is executed
     – doInBackground(Param ...): invoked on the
       background thread immediately after
       onPreExecute() finishes executing
     – onProgressUpdate(Progress...): invoked on the
       UI thread after a call to
       publishProgress(Progress...)
     – onPostExecute(Result): invoked on the UI
       thread after the background computation
       finishes
AsyncTask example
•   Violate the single thread model: the Android UI toolkit is not
    thread-safe and must always be manipulated on the UI thread.
•   In this piece of code, the ImageView is manipulated on a
    worker thread, which can cause really weird problems. Tracking
    down and fixing such bugs can be difficult and time-consuming
•   Classes and methods also tend to make the code more
    complicated and more difficult to read.
•   It becomes even worse when our implement complex
    operations that require frequent UI updates
Android - Thread, Handler and AsyncTask
Android - Thread, Handler and AsyncTask
Ad

More Related Content

What's hot (20)

Android Services
Android ServicesAndroid Services
Android Services
Ahsanul Karim
 
SQLITE Android
SQLITE AndroidSQLITE Android
SQLITE Android
Sourabh Sahu
 
Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android Development
Aly Abdelkareem
 
Android activity lifecycle
Android activity lifecycleAndroid activity lifecycle
Android activity lifecycle
Soham Patel
 
Android Layout.pptx
Android Layout.pptxAndroid Layout.pptx
Android Layout.pptx
vishal choudhary
 
Android Application Development Using Java
Android Application Development Using JavaAndroid Application Development Using Java
Android Application Development Using Java
amaankhan
 
Android activities & views
Android activities & viewsAndroid activities & views
Android activities & views
ma-polimi
 
Android intents
Android intentsAndroid intents
Android intents
Siva Ramakrishna kv
 
Android ui menu
Android ui menuAndroid ui menu
Android ui menu
Krazy Koder
 
Android Development with Kotlin, Part 1 - Introduction
Android Development with Kotlin, Part 1 - IntroductionAndroid Development with Kotlin, Part 1 - Introduction
Android Development with Kotlin, Part 1 - Introduction
Andreas Jakl
 
Notification android
Notification androidNotification android
Notification android
ksheerod shri toshniwal
 
Introduction to fragments in android
Introduction to fragments in androidIntroduction to fragments in android
Introduction to fragments in android
Prawesh Shrestha
 
Intents in Android
Intents in AndroidIntents in Android
Intents in Android
ma-polimi
 
Android Telephony Manager and SMS
Android Telephony Manager and SMSAndroid Telephony Manager and SMS
Android Telephony Manager and SMS
Jussi Pohjolainen
 
Junit
JunitJunit
Junit
FAROOK Samath
 
Android application structure
Android application structureAndroid application structure
Android application structure
Alexey Ustenko
 
Android notification
Android notificationAndroid notification
Android notification
Krazy Koder
 
Flutter
FlutterFlutter
Flutter
Himanshu Singh
 
Android User Interface: Basic Form Widgets
Android User Interface: Basic Form WidgetsAndroid User Interface: Basic Form Widgets
Android User Interface: Basic Form Widgets
Ahsanul Karim
 
Introduction to Android and Android Studio
Introduction to Android and Android StudioIntroduction to Android and Android Studio
Introduction to Android and Android Studio
Suyash Srijan
 
Introduction to Android Development
Introduction to Android DevelopmentIntroduction to Android Development
Introduction to Android Development
Aly Abdelkareem
 
Android activity lifecycle
Android activity lifecycleAndroid activity lifecycle
Android activity lifecycle
Soham Patel
 
Android Application Development Using Java
Android Application Development Using JavaAndroid Application Development Using Java
Android Application Development Using Java
amaankhan
 
Android activities & views
Android activities & viewsAndroid activities & views
Android activities & views
ma-polimi
 
Android Development with Kotlin, Part 1 - Introduction
Android Development with Kotlin, Part 1 - IntroductionAndroid Development with Kotlin, Part 1 - Introduction
Android Development with Kotlin, Part 1 - Introduction
Andreas Jakl
 
Introduction to fragments in android
Introduction to fragments in androidIntroduction to fragments in android
Introduction to fragments in android
Prawesh Shrestha
 
Intents in Android
Intents in AndroidIntents in Android
Intents in Android
ma-polimi
 
Android Telephony Manager and SMS
Android Telephony Manager and SMSAndroid Telephony Manager and SMS
Android Telephony Manager and SMS
Jussi Pohjolainen
 
Android application structure
Android application structureAndroid application structure
Android application structure
Alexey Ustenko
 
Android notification
Android notificationAndroid notification
Android notification
Krazy Koder
 
Android User Interface: Basic Form Widgets
Android User Interface: Basic Form WidgetsAndroid User Interface: Basic Form Widgets
Android User Interface: Basic Form Widgets
Ahsanul Karim
 
Introduction to Android and Android Studio
Introduction to Android and Android StudioIntroduction to Android and Android Studio
Introduction to Android and Android Studio
Suyash Srijan
 

Similar to Android - Thread, Handler and AsyncTask (20)

Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
ssuserfcae42
 
Object-Oriented-Prog_MultiThreading.pptx
Object-Oriented-Prog_MultiThreading.pptxObject-Oriented-Prog_MultiThreading.pptx
Object-Oriented-Prog_MultiThreading.pptx
NasreenTaj20
 
Inter thread communication &amp; runnable interface
Inter thread communication &amp; runnable interfaceInter thread communication &amp; runnable interface
Inter thread communication &amp; runnable interface
keval_thummar
 
Scheduling Thread
Scheduling  ThreadScheduling  Thread
Scheduling Thread
MuhammadBilal187526
 
U4 JAVA.pptx
U4 JAVA.pptxU4 JAVA.pptx
U4 JAVA.pptx
madan r
 
Multithreading
MultithreadingMultithreading
Multithreading
SanthiNivas
 
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreading
ssusere538f7
 
advanced java programming paradigms presentation
advanced java programming paradigms presentationadvanced java programming paradigms presentation
advanced java programming paradigms presentation
PriyadharshiniG41
 
Multithreading
MultithreadingMultithreading
Multithreading
backdoor
 
Module 4 - Part 4 - Multithreaded Programming.pptx
Module 4 - Part 4 - Multithreaded Programming.pptxModule 4 - Part 4 - Multithreaded Programming.pptx
Module 4 - Part 4 - Multithreaded Programming.pptx
FahmaFamzin
 
MULTITHREADING PROGRAMMING AND I/O THREAD
MULTITHREADING PROGRAMMING AND I/O THREADMULTITHREADING PROGRAMMING AND I/O THREAD
MULTITHREADING PROGRAMMING AND I/O THREAD
mohanrajm63
 
[Java concurrency]01.thread management
[Java concurrency]01.thread management[Java concurrency]01.thread management
[Java concurrency]01.thread management
xuehan zhu
 
Java threads
Java threadsJava threads
Java threads
Prabhakaran V M
 
Md09 multithreading
Md09 multithreadingMd09 multithreading
Md09 multithreading
Rakesh Madugula
 
Multithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdfMultithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdf
Harika Pudugosula
 
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptx
Arulmozhivarman8
 
Threading.pptx
Threading.pptxThreading.pptx
Threading.pptx
BalasundaramSr
 
0903 1
0903 10903 1
0903 1
petrcoy
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
VINOTH R
 
Multithreading and concurrency.pptx
Multithreading and concurrency.pptxMultithreading and concurrency.pptx
Multithreading and concurrency.pptx
ShymmaaQadoom1
 
Multithreading.pptx
Multithreading.pptxMultithreading.pptx
Multithreading.pptx
ssuserfcae42
 
Object-Oriented-Prog_MultiThreading.pptx
Object-Oriented-Prog_MultiThreading.pptxObject-Oriented-Prog_MultiThreading.pptx
Object-Oriented-Prog_MultiThreading.pptx
NasreenTaj20
 
Inter thread communication &amp; runnable interface
Inter thread communication &amp; runnable interfaceInter thread communication &amp; runnable interface
Inter thread communication &amp; runnable interface
keval_thummar
 
U4 JAVA.pptx
U4 JAVA.pptxU4 JAVA.pptx
U4 JAVA.pptx
madan r
 
Threads in java, Multitasking and Multithreading
Threads in java, Multitasking and MultithreadingThreads in java, Multitasking and Multithreading
Threads in java, Multitasking and Multithreading
ssusere538f7
 
advanced java programming paradigms presentation
advanced java programming paradigms presentationadvanced java programming paradigms presentation
advanced java programming paradigms presentation
PriyadharshiniG41
 
Multithreading
MultithreadingMultithreading
Multithreading
backdoor
 
Module 4 - Part 4 - Multithreaded Programming.pptx
Module 4 - Part 4 - Multithreaded Programming.pptxModule 4 - Part 4 - Multithreaded Programming.pptx
Module 4 - Part 4 - Multithreaded Programming.pptx
FahmaFamzin
 
MULTITHREADING PROGRAMMING AND I/O THREAD
MULTITHREADING PROGRAMMING AND I/O THREADMULTITHREADING PROGRAMMING AND I/O THREAD
MULTITHREADING PROGRAMMING AND I/O THREAD
mohanrajm63
 
[Java concurrency]01.thread management
[Java concurrency]01.thread management[Java concurrency]01.thread management
[Java concurrency]01.thread management
xuehan zhu
 
Multithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdfMultithreaded Programming Part- II.pdf
Multithreaded Programming Part- II.pdf
Harika Pudugosula
 
OOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptxOOPS object oriented programming UNIT-4.pptx
OOPS object oriented programming UNIT-4.pptx
Arulmozhivarman8
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
VINOTH R
 
Multithreading and concurrency.pptx
Multithreading and concurrency.pptxMultithreading and concurrency.pptx
Multithreading and concurrency.pptx
ShymmaaQadoom1
 
Ad

Recently uploaded (20)

Datastucture-Unit 4-Linked List Presentation.pptx
Datastucture-Unit 4-Linked List Presentation.pptxDatastucture-Unit 4-Linked List Presentation.pptx
Datastucture-Unit 4-Linked List Presentation.pptx
kaleeswaric3
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
"PHP and MySQL CRUD Operations for Student Management System"
"PHP and MySQL CRUD Operations for Student Management System""PHP and MySQL CRUD Operations for Student Management System"
"PHP and MySQL CRUD Operations for Student Management System"
Jainul Musani
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
Salesforce AI Associate 2 of 2 Certification.docx
Salesforce AI Associate 2 of 2 Certification.docxSalesforce AI Associate 2 of 2 Certification.docx
Salesforce AI Associate 2 of 2 Certification.docx
José Enrique López Rivera
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Rock, Paper, Scissors: An Apex Map Learning Journey
Rock, Paper, Scissors: An Apex Map Learning JourneyRock, Paper, Scissors: An Apex Map Learning Journey
Rock, Paper, Scissors: An Apex Map Learning Journey
Lynda Kane
 
Mobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi ArabiaMobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi Arabia
Steve Jonas
 
Datastucture-Unit 4-Linked List Presentation.pptx
Datastucture-Unit 4-Linked List Presentation.pptxDatastucture-Unit 4-Linked List Presentation.pptx
Datastucture-Unit 4-Linked List Presentation.pptx
kaleeswaric3
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
"PHP and MySQL CRUD Operations for Student Management System"
"PHP and MySQL CRUD Operations for Student Management System""PHP and MySQL CRUD Operations for Student Management System"
"PHP and MySQL CRUD Operations for Student Management System"
Jainul Musani
 
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdfComplete Guide to Advanced Logistics Management Software in Riyadh.pdf
Complete Guide to Advanced Logistics Management Software in Riyadh.pdf
Software Company
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
Salesforce AI Associate 2 of 2 Certification.docx
Salesforce AI Associate 2 of 2 Certification.docxSalesforce AI Associate 2 of 2 Certification.docx
Salesforce AI Associate 2 of 2 Certification.docx
José Enrique López Rivera
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdfThe Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
The Evolution of Meme Coins A New Era for Digital Currency ppt.pdf
Abi john
 
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Enhancing ICU Intelligence: How Our Functional Testing Enabled a Healthcare I...
Impelsys Inc.
 
Rock, Paper, Scissors: An Apex Map Learning Journey
Rock, Paper, Scissors: An Apex Map Learning JourneyRock, Paper, Scissors: An Apex Map Learning Journey
Rock, Paper, Scissors: An Apex Map Learning Journey
Lynda Kane
 
Mobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi ArabiaMobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi Arabia
Steve Jonas
 
Ad

Android - Thread, Handler and AsyncTask

  • 1. Android Thread, Handler and AsyncTask Hoang Ngo
  • 2. Thread • Act much like usual Java Threads • Can’t act directly on external User Interface objects (throw the Exception CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views”) • Can’t be stopped by executing destroy() nor stop(). Use instead interrupt() or join() (by case)
  • 3. Thread • Two main ways of having a Thread execute application code: – Providing a new class that extends Thread and overriding its run() method. – Providing a new Thread instance with a Runnable object during its creation. In both cases, the start() method must be called to actually execute the new Thread.
  • 5. Handler • Associated with a single thread and that thread's message queue • Bound to the thread / message queue of the thread that is creating it • Deliver messages and runnables to that message queue • Execute them as they come out of the message queue
  • 6. Handler • Two main uses for a Handler: – To schedule messages and runnables to be executed as some point in the future – To add an action into a queue performed on a different thread
  • 8. AsyncTask • Created on the UI thread and can be executed only once • Run on a background thread and result is published on the UI thread • The three types used by an asynchronous task are the following – Params, the type of the parameters sent to the task upon execution – Progress, the type of the progress units published during the background computation – Result, the type of the result of the background computation • Extend AsyncTask<Void, Void, Void>
  • 9. AsyncTask • Go through 4 steps: – onPreExecute(): invoked on the UI thread immediately after the task is executed – doInBackground(Param ...): invoked on the background thread immediately after onPreExecute() finishes executing – onProgressUpdate(Progress...): invoked on the UI thread after a call to publishProgress(Progress...) – onPostExecute(Result): invoked on the UI thread after the background computation finishes
  • 11. Violate the single thread model: the Android UI toolkit is not thread-safe and must always be manipulated on the UI thread. • In this piece of code, the ImageView is manipulated on a worker thread, which can cause really weird problems. Tracking down and fixing such bugs can be difficult and time-consuming
  • 12. Classes and methods also tend to make the code more complicated and more difficult to read. • It becomes even worse when our implement complex operations that require frequent UI updates