SlideShare a Scribd company logo
4
Most read
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
Hoang Ngo
 
PPT
Voicexml
navyabelli
 
PPTX
Android Binder: Deep Dive
Zafar Shahid, PhD
 
PPTX
Dynamic method dispatch
yugandhar vadlamudi
 
PPT
Android | Android Activity Launch Modes and Tasks | Gonçalo Silva
JAX London
 
PPTX
Android Services
Ahsanul Karim
 
PDF
Multithreading in Android
coolmirza143
 
PPTX
Android studio installation
PoojaBele1
 
Android - Thread, Handler and AsyncTask
Hoang Ngo
 
Voicexml
navyabelli
 
Android Binder: Deep Dive
Zafar Shahid, PhD
 
Dynamic method dispatch
yugandhar vadlamudi
 
Android | Android Activity Launch Modes and Tasks | Gonçalo Silva
JAX London
 
Android Services
Ahsanul Karim
 
Multithreading in Android
coolmirza143
 
Android studio installation
PoojaBele1
 

What's hot (20)

PPTX
Android testing
JinaTm
 
PPTX
JavaScript Promises
L&T Technology Services Limited
 
PPTX
Concurrency & Parallel Programming
Ramazan AYYILDIZ
 
PDF
Android animation
Krazy Koder
 
PPTX
UNIX Operating System
Fatima Qayyum
 
PPT
The Evolution of Java
Fu Cheng
 
PPTX
Java constructors
QUONTRASOLUTIONS
 
PDF
Using and Customizing the Android Framework / part 4 of Embedded Android Work...
Opersys inc.
 
PPTX
04 activities and activity life cycle
Sokngim Sa
 
PPTX
JAVA AWT
shanmuga rajan
 
PPT
android layouts
Deepa Rani
 
PPTX
Dynamic Polymorphism in C++
Dharmisha Sharma
 
PDF
Android life cycle
瑋琮 林
 
PPTX
Creating the first app with android studio
Parinita03
 
PPTX
Arrays in Java
Abhilash Nair
 
PDF
Java Thread Synchronization
Benj Del Mundo
 
PPTX
Interfaces in java
Abishek Purushothaman
 
PPTX
Decorator Pattern
Dimuthu Anuraj
 
PPTX
Reactjs workshop
Ahmed rebai
 
Android testing
JinaTm
 
JavaScript Promises
L&T Technology Services Limited
 
Concurrency & Parallel Programming
Ramazan AYYILDIZ
 
Android animation
Krazy Koder
 
UNIX Operating System
Fatima Qayyum
 
The Evolution of Java
Fu Cheng
 
Java constructors
QUONTRASOLUTIONS
 
Using and Customizing the Android Framework / part 4 of Embedded Android Work...
Opersys inc.
 
04 activities and activity life cycle
Sokngim Sa
 
JAVA AWT
shanmuga rajan
 
android layouts
Deepa Rani
 
Dynamic Polymorphism in C++
Dharmisha Sharma
 
Android life cycle
瑋琮 林
 
Creating the first app with android studio
Parinita03
 
Arrays in Java
Abhilash Nair
 
Java Thread Synchronization
Benj Del Mundo
 
Interfaces in java
Abishek Purushothaman
 
Decorator Pattern
Dimuthu Anuraj
 
Reactjs workshop
Ahmed rebai
 
Ad

Viewers also liked (17)

PPTX
Efficient Android Threading
Anders Göransson
 
PDF
10 ways to improve your Android app performance
Boris Farber
 
ODP
Android App Development - 07 Threading
Diego Grancini
 
PPTX
Android Dialogs Tutorial
Perfect APK
 
PPTX
Android service, aidl - day 1
Utkarsh Mankad
 
PDF
Deep dive into android restoration - DroidCon Paris 2014
Paris Android User Group
 
PPTX
Thread management
Ayaan Adeel
 
PPT
Lecture Slides for Preferences and Menus [Android ]
Nehil Jain
 
PDF
Android service
Kirill Rozov
 
PDF
Android Threading
Jussi Pohjolainen
 
PDF
AndroidManifest
Ahsanul Karim
 
PPTX
Android intents, notification and broadcast recievers
Utkarsh Mankad
 
PDF
Android intents
Siva Ramakrishna kv
 
KEY
Android Development: The Basics
Mike Desjardins
 
PPTX
Android - Intents - Mazenet Solution
Mazenetsolution
 
PDF
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Xavier Hallade
 
PPTX
Creating apps that work on all screen sizes
Ravi Vyas
 
Efficient Android Threading
Anders Göransson
 
10 ways to improve your Android app performance
Boris Farber
 
Android App Development - 07 Threading
Diego Grancini
 
Android Dialogs Tutorial
Perfect APK
 
Android service, aidl - day 1
Utkarsh Mankad
 
Deep dive into android restoration - DroidCon Paris 2014
Paris Android User Group
 
Thread management
Ayaan Adeel
 
Lecture Slides for Preferences and Menus [Android ]
Nehil Jain
 
Android service
Kirill Rozov
 
Android Threading
Jussi Pohjolainen
 
AndroidManifest
Ahsanul Karim
 
Android intents, notification and broadcast recievers
Utkarsh Mankad
 
Android intents
Siva Ramakrishna kv
 
Android Development: The Basics
Mike Desjardins
 
Android - Intents - Mazenet Solution
Mazenetsolution
 
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Xavier Hallade
 
Creating apps that work on all screen sizes
Ravi Vyas
 
Ad

Similar to Android async task (20)

PDF
Android development training programme , Day 3
DHIRAJ PRAVIN
 
PDF
Session 9 Android Web Services - Part 2.pdf
EngmohammedAlzared
 
PPTX
Android AsyncTask Tutorial
Perfect APK
 
PPT
Multithreading
backdoor
 
PDF
Internals of AsyncTask
BlrDroid
 
PPTX
Android session-5-sajib
Hussain Behestee
 
PPTX
Background Thread
Anjan Debnath
 
PDF
Improving app performance with Kotlin Coroutines
Hassan Abid
 
PDF
UNIT-3.pdf, buffer module, treams,file accessing using node js
chandrapuraja
 
PPTX
Android Connecting to internet Part 2
Paramvir Singh
 
PDF
[Android] Multiple Background Threads
Nikmesoft Ltd
 
PPTX
Threads handlers and async task, widgets - day8
Utkarsh Mankad
 
PPTX
Will it run or will it not run? Background processes in Android 6 - Anna Lifs...
DroidConTLV
 
PDF
Android Activities.pdf
ssusere71a07
 
PDF
Asynchronous Programming in Android
John Pendexter
 
PPTX
Multithreading.pptx
ssuserfcae42
 
PDF
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
Mohammad Reza Kamalifard
 
PDF
Android Best Practices - Thoughts from the Trenches
Anuradha Weeraman
 
PDF
MobileAppDev Handout#4
trupti1976
 
PPT
Threads in Java
Gaurav Aggarwal
 
Android development training programme , Day 3
DHIRAJ PRAVIN
 
Session 9 Android Web Services - Part 2.pdf
EngmohammedAlzared
 
Android AsyncTask Tutorial
Perfect APK
 
Multithreading
backdoor
 
Internals of AsyncTask
BlrDroid
 
Android session-5-sajib
Hussain Behestee
 
Background Thread
Anjan Debnath
 
Improving app performance with Kotlin Coroutines
Hassan Abid
 
UNIT-3.pdf, buffer module, treams,file accessing using node js
chandrapuraja
 
Android Connecting to internet Part 2
Paramvir Singh
 
[Android] Multiple Background Threads
Nikmesoft Ltd
 
Threads handlers and async task, widgets - day8
Utkarsh Mankad
 
Will it run or will it not run? Background processes in Android 6 - Anna Lifs...
DroidConTLV
 
Android Activities.pdf
ssusere71a07
 
Asynchronous Programming in Android
John Pendexter
 
Multithreading.pptx
ssuserfcae42
 
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
Mohammad Reza Kamalifard
 
Android Best Practices - Thoughts from the Trenches
Anuradha Weeraman
 
MobileAppDev Handout#4
trupti1976
 
Threads in Java
Gaurav Aggarwal
 

Recently uploaded (20)

PDF
Key_Statistical_Techniques_in_Analytics_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PDF
Research about a FoodFolio app for personalized dietary tracking and health o...
AustinLiamAndres
 
PDF
TIC ACTIVIDAD 1geeeeeeeeeeeeeeeeeeeeeeeeeeeeeer3.pdf
Thais Ruiz
 
PPTX
Data Security Breach: Immediate Action Plan
varmabhuvan266
 
PDF
oop_java (1) of ice or cse or eee ic.pdf
sabiquntoufiqlabonno
 
PPTX
Data-Driven Machine Learning for Rail Infrastructure Health Monitoring
Sione Palu
 
PDF
345_IT infrastructure for business management.pdf
LEANHTRAN4
 
PPTX
Complete_STATA_Introduction_Beginner.pptx
mbayekebe
 
PPT
Grade 5 PPT_Science_Q2_W6_Methods of reproduction.ppt
AaronBaluyut
 
PDF
A Systems Thinking Approach to Algorithmic Fairness.pdf
Epistamai
 
PPTX
Web_Engineering_Assignment_Clean.pptxfor college
HUSNAINAHMAD39
 
PPTX
Presentation (1) (1).pptx k8hhfftuiiigff
karthikjagath2005
 
PPTX
Purple and Violet Modern Marketing Presentation (1).pptx
SanthoshKumar229321
 
PPTX
Introduction to Biostatistics Presentation.pptx
AtemJoshua
 
PDF
The_Future_of_Data_Analytics_by_CA_Suvidha_Chaplot_UPDATED.pdf
CA Suvidha Chaplot
 
PPTX
Introduction to Data Analytics and Data Science
KavithaCIT
 
PPT
2009worlddatasheet_presentation.ppt peoole
umutunsalnsl4402
 
PPTX
lecture 13 mind test academy it skills.pptx
ggesjmrasoolpark
 
PPTX
Introduction-to-Python-Programming-Language (1).pptx
dhyeysapariya
 
PDF
Technical Writing Module-I Complete Notes.pdf
VedprakashArya13
 
Key_Statistical_Techniques_in_Analytics_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
Research about a FoodFolio app for personalized dietary tracking and health o...
AustinLiamAndres
 
TIC ACTIVIDAD 1geeeeeeeeeeeeeeeeeeeeeeeeeeeeeer3.pdf
Thais Ruiz
 
Data Security Breach: Immediate Action Plan
varmabhuvan266
 
oop_java (1) of ice or cse or eee ic.pdf
sabiquntoufiqlabonno
 
Data-Driven Machine Learning for Rail Infrastructure Health Monitoring
Sione Palu
 
345_IT infrastructure for business management.pdf
LEANHTRAN4
 
Complete_STATA_Introduction_Beginner.pptx
mbayekebe
 
Grade 5 PPT_Science_Q2_W6_Methods of reproduction.ppt
AaronBaluyut
 
A Systems Thinking Approach to Algorithmic Fairness.pdf
Epistamai
 
Web_Engineering_Assignment_Clean.pptxfor college
HUSNAINAHMAD39
 
Presentation (1) (1).pptx k8hhfftuiiigff
karthikjagath2005
 
Purple and Violet Modern Marketing Presentation (1).pptx
SanthoshKumar229321
 
Introduction to Biostatistics Presentation.pptx
AtemJoshua
 
The_Future_of_Data_Analytics_by_CA_Suvidha_Chaplot_UPDATED.pdf
CA Suvidha Chaplot
 
Introduction to Data Analytics and Data Science
KavithaCIT
 
2009worlddatasheet_presentation.ppt peoole
umutunsalnsl4402
 
lecture 13 mind test academy it skills.pptx
ggesjmrasoolpark
 
Introduction-to-Python-Programming-Language (1).pptx
dhyeysapariya
 
Technical Writing Module-I Complete Notes.pdf
VedprakashArya13
 

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);