SlideShare a Scribd company logo
Android AsyncTask
08-12-2012
Introduction to AsycnTask
AsyncTask is an abstract class provided by Android which
helps us to use the UI thread properly. This class allows us to
perform long/background operations and show its result on
the UI thread without having to manipulate threads.
AsyncTask Methods
• doInBackground: Code performing long running operation
goes in this method. When onClick method is executed on
click of button, it calls execute method which accepts
parameters and automatically calls doInBackground
method with the parameters passed.
• onPostExecute: This method is called
after doInBackground method completes processing. Result
from doInBackground is passed to this method.
• onPreExecute: This method is called
before doInBackground method is called.
• onProgressUpdate: This method is invoked by
calling publishProgress anytime from doInBackground call
this method.
AsyncTask Example
private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {
protected Long doInBackground(URL... urls) {
int count = urls.length;
long totalSize = 0;
for (int i = 0; i < count; i++) {
totalSize += Downloader.downloadFile(urls[i]);
publishProgress((int) ((i / (float) count) * 100));
// Escape early if cancel() is called
if (isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Long result) {
showDialog("Downloaded " + result + " bytes");
}
}
AsyncTask Execute
3 Parameters for the Asynctask
• Params – parameters sent to the task upon execution
• Progress – progress units published during background execution
• Result – result of the task after completion
private class MyTask extends
AsyncTask<Void, Void, Void> { ... }
Unused parameters can be set as void
New DownloadfilesTasks().execute (URL, null, null);

More Related Content

PPT
Android - Thread, Handler and AsyncTask
PPTX
Introduction to Android and Android Studio
PPTX
Android Intent.pptx
PPTX
Broadcast Receiver
PDF
Basics and different xml files used in android
PPTX
Presentation on Core java
PPTX
Android app development ppt
Android - Thread, Handler and AsyncTask
Introduction to Android and Android Studio
Android Intent.pptx
Broadcast Receiver
Basics and different xml files used in android
Presentation on Core java
Android app development ppt

What's hot (20)

PPTX
Basics of JAVA programming
PDF
Android intents
PPTX
Java reflection
PPTX
Android Operating System
PPT
Java Presentation
PPTX
Servlets
PPS
Java Exception handling
PPTX
PPTX
Java Beans
PDF
Android animation
PDF
Android activities & views
PDF
Android notification
PPTX
Introduction to MERN Stack
PDF
Introduction of Xcode
PPTX
Recycler view
PPTX
Java awt (abstract window toolkit)
PPTX
oops concept in java | object oriented programming in java
PPTX
Mobile Application Testing
PDF
Basics of JAVA programming
Android intents
Java reflection
Android Operating System
Java Presentation
Servlets
Java Exception handling
Java Beans
Android animation
Android activities & views
Android notification
Introduction to MERN Stack
Introduction of Xcode
Recycler view
Java awt (abstract window toolkit)
oops concept in java | object oriented programming in java
Mobile Application Testing
Ad

Viewers also liked (16)

PPTX
Efficient Android Threading
PDF
10 ways to improve your Android app performance
ODP
Android App Development - 07 Threading
PPTX
Android Dialogs Tutorial
PPTX
Android service, aidl - day 1
PDF
Deep dive into android restoration - DroidCon Paris 2014
PPTX
Thread management
PPT
Lecture Slides for Preferences and Menus [Android ]
PDF
Android service
PDF
Android Threading
PDF
AndroidManifest
PPTX
Android intents, notification and broadcast recievers
KEY
Android Development: The Basics
PPTX
Android - Intents - Mazenet Solution
PDF
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
PPTX
Creating apps that work on all screen sizes
Efficient Android Threading
10 ways to improve your Android app performance
Android App Development - 07 Threading
Android Dialogs Tutorial
Android service, aidl - day 1
Deep dive into android restoration - DroidCon Paris 2014
Thread management
Lecture Slides for Preferences and Menus [Android ]
Android service
Android Threading
AndroidManifest
Android intents, notification and broadcast recievers
Android Development: The Basics
Android - Intents - Mazenet Solution
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Creating apps that work on all screen sizes
Ad

Similar to Android async task (20)

PDF
Android development training programme , Day 3
PDF
Session 9 Android Web Services - Part 2.pdf
PPTX
Android AsyncTask Tutorial
PPT
Multithreading
PDF
Internals of AsyncTask
PPTX
Android session-5-sajib
PPTX
Background Thread
PDF
Improving app performance with Kotlin Coroutines
PDF
UNIT-3.pdf, buffer module, treams,file accessing using node js
PPTX
Android Connecting to internet Part 2
PDF
[Android] Multiple Background Threads
PPTX
Threads handlers and async task, widgets - day8
PPTX
Will it run or will it not run? Background processes in Android 6 - Anna Lifs...
PDF
Android Activities.pdf
PDF
Asynchronous Programming in Android
PPTX
Multithreading.pptx
PDF
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
PDF
Android Best Practices - Thoughts from the Trenches
PDF
MobileAppDev Handout#4
PPT
Threads in Java
Android development training programme , Day 3
Session 9 Android Web Services - Part 2.pdf
Android AsyncTask Tutorial
Multithreading
Internals of AsyncTask
Android session-5-sajib
Background Thread
Improving app performance with Kotlin Coroutines
UNIT-3.pdf, buffer module, treams,file accessing using node js
Android Connecting to internet Part 2
[Android] Multiple Background Threads
Threads handlers and async task, widgets - day8
Will it run or will it not run? Background processes in Android 6 - Anna Lifs...
Android Activities.pdf
Asynchronous Programming in Android
Multithreading.pptx
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
Android Best Practices - Thoughts from the Trenches
MobileAppDev Handout#4
Threads in Java

Recently uploaded (20)

PDF
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PDF
Foundation of Data Science unit number two notes
PDF
Mega Projects Data Mega Projects Data
PDF
Taxes Foundatisdcsdcsdon Certificate.pdf
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
PDF
.pdf is not working space design for the following data for the following dat...
PPTX
CEE 2 REPORT G7.pptxbdbshjdgsgjgsjfiuhsd
PPTX
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
PPT
Reliability_Chapter_ presentation 1221.5784
PPT
Quality review (1)_presentation of this 21
PPT
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
PPT
Miokarditis (Inflamasi pada Otot Jantung)
PPT
Chapter 2 METAL FORMINGhhhhhhhjjjjmmmmmmmmm
PPTX
Introduction to Knowledge Engineering Part 1
PDF
Clinical guidelines as a resource for EBP(1).pdf
PDF
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
PPTX
Global journeys: estimating international migration
PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
“Getting Started with Data Analytics Using R – Concepts, Tools & Case Studies”
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
Foundation of Data Science unit number two notes
Mega Projects Data Mega Projects Data
Taxes Foundatisdcsdcsdon Certificate.pdf
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
.pdf is not working space design for the following data for the following dat...
CEE 2 REPORT G7.pptxbdbshjdgsgjgsjfiuhsd
Bharatiya Antariksh Hackathon 2025 Idea Submission PPT.pptx
Reliability_Chapter_ presentation 1221.5784
Quality review (1)_presentation of this 21
Chapter 3 METAL JOINING.pptnnnnnnnnnnnnn
Miokarditis (Inflamasi pada Otot Jantung)
Chapter 2 METAL FORMINGhhhhhhhjjjjmmmmmmmmm
Introduction to Knowledge Engineering Part 1
Clinical guidelines as a resource for EBP(1).pdf
BF and FI - Blockchain, fintech and Financial Innovation Lesson 2.pdf
Global journeys: estimating international migration
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg

Android async task

  • 2. Introduction to AsycnTask AsyncTask is an abstract class provided by Android which helps us to use the UI thread properly. This class allows us to perform long/background operations and show its result on the UI thread without having to manipulate threads.
  • 3. AsyncTask Methods • doInBackground: Code performing long running operation goes in this method. When onClick method is executed on click of button, it calls execute method which accepts parameters and automatically calls doInBackground method with the parameters passed. • onPostExecute: This method is called after doInBackground method completes processing. Result from doInBackground is passed to this method. • onPreExecute: This method is called before doInBackground method is called. • onProgressUpdate: This method is invoked by calling publishProgress anytime from doInBackground call this method.
  • 4. AsyncTask Example private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> { protected Long doInBackground(URL... urls) { int count = urls.length; long totalSize = 0; for (int i = 0; i < count; i++) { totalSize += Downloader.downloadFile(urls[i]); publishProgress((int) ((i / (float) count) * 100)); // Escape early if cancel() is called if (isCancelled()) break; } return totalSize; } protected void onProgressUpdate(Integer... progress) { setProgressPercent(progress[0]); } protected void onPostExecute(Long result) { showDialog("Downloaded " + result + " bytes"); } }
  • 5. AsyncTask Execute 3 Parameters for the Asynctask • Params – parameters sent to the task upon execution • Progress – progress units published during background execution • Result – result of the task after completion private class MyTask extends AsyncTask<Void, Void, Void> { ... } Unused parameters can be set as void New DownloadfilesTasks().execute (URL, null, null);