Android Tutorial – Using AsyncTask for Android With Simple Example for Beginners

Seegatesite – I’ll share how to use AsyncTask for android. What does it AsyncTask? AsyncTask is one of the features in Android Programming for executing the operation to be performed in the background. So basically operations are performed in the background thread and then when it is finished it will be directly displayed in the UI. Either successful or unsuccessful in accordance with the pre-arranged. In the previous article about xml parser android, I use the AsyncTask method for which we will execute the xml file. How important AsyncTask for android application?

According to the Android Developers’s Site

AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask.

In essence, the process AsyncTask background done in a separate thread.

Okay, for more details, we immediately began the following experiment

Creating a simple application using AsyncTask for android 

1. Create a new android project named AsynctaskTutorial, read how to create new android project in android studio

2. Add TextView, Progress Bar, and Button on activity_main.xml

Android Tutorial Using Asynctask Android For Beginner

3. Add the following script to

4. Run App And Click the Process Button

Android Tutorial Using Asynctask With Simple Example Android

Explanation :

1. onPreExecute()

in this function we prepare early initialization settings, before the operation is executed. In the example above, we initialize progress_status = 0 and display TextView with the word “Processing 0%”. After the process onPreExecute() is complete, the process is continued with doInBackground().

2. doInBackground()

The part where the operations are performed in the background. In the example above will be looping to add the value of progressbar. The value of the progress bar will be displayed to the UI with publishProgress’s method.

3. onProgressUpdate()

when the operating process runs every change will be displayed. onProgressUpdate () will capture the data that is given by the method publishProgress and shown to the UI. In the example above, OnProgressUpdate() will execute progressBar.setProgress(values[0]) and txtpercentage.setText(“Processing ” +values[0]+”%”)

4. onPostExecute()

after the process / operation is completed this function is executed

Thus article about Android Tutorial – Using AsyncTask for Android With Simple Example for Beginners, hope useful.

if you need more example how to use asynctask for android , please read my another article Android Tutorial – How to Use XML Parser Android with Simple Example for Beginner

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.