Android


Android Asynctask example with Progress Bar

Asynctask example with Progress Bar

Android AsyncTask (Asynchronous Task) is an abstract class. In this tutorial, I’ll show you, how to use Asynctask example with Progress Bar. Asynctask provided by Android which gives us the liberty to perform heavy tasks in the background and keep the UI thread light thus making the application more responsive and fast.

AsyncTask class is used to do background operations that will update the UI(user interface). Mainly we used it for short operations that will not effect on our main thread.

The android application runs on a single thread when launched. Due to this single thread model tasks that take longer time to fetch the response can make the application non-responsive.

To avoid this we use android AsyncTask to perform the heavy tasks in background on a dedicated thread and passing the results back to the UI thread. Hence use of AsyncTask in android application keeps the UI thread responsive at all times.

Asynchronous task execution steps

  • onPreExecute
  • doInBackground(Params…)
  • onProgressUpdate(Progress…)
  • onPostExecute(Result)

First of all AsyncTask class is firstly executed using execute() method. Firstly AsyncTask is called onPreExecute() after that onPreExecute() calls doInBackground() for background processes and then doInBackground() calls onPostExecute() method to update the UI.

Asynchronous task execution steps

onPreExecute() :

This invoked on the UI thread before the task is executed. The method contains the code, executed before the background processing starts. Normally, used to set up the task, for instance by showing a progress bar in the user interface.

doInBackground(Params…):

This method contains the code which needs to be executed in the background. In this method, we can send results multiple times to the UI thread by publishProgress() method. Finally, we will the return statements to notify that the background processing has been completed.

This invoked on the background thread immediately after onPreExecute() finishes executing. Normally used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step.

onProgressUpdate(Progress…):

This method receives progress updates from the doInBackground method. It runs on the UI thread, which is published via the publishProgress method. In this method can use this progress update to update the UI thread. This method is used to display any form of progress in the user interface. When the background computation executing. You can use Progress Bar or show Toast for users.

onPostExecute(Result):

This method is called after doInBackground method computation finishes. Normally used on the UI thread. The result of the background computation is passed to this step as a parameter. You can use the progress bar or show Toast for users.

Syntax code for Android AsyncTask

In the above snippet, we’ve used a sample class name that extends AsyncTask and executes method.

  • The AsyncTask instance needs to created and invoked in the UI thread.
  • The overridden methods in the AsyncTask class called automatically.
  • AsyncTask called only once at a time. Executing it again will throw an exception.
  • You can change the return parameter type as well as request parameter type according to your requirement.

Asynctask example with Progress Bar

Let’s take an example of Asynctask example with Progress Bar that makes a process to go to sleep for a given period of time as set by the user.

AndroidManifest

activity_main

MainActivity

MyAsyncTask

In the above code, we’ve used AsyncTaskRunner class to perform the AsyncTask operations. The time in seconds is passed as a parameter to the class and a ProgressDialog is displayed for the given amount of time.

Advantages of Android AsyncTask

Android AsyncTask have many advantages for users to use this, Let’s take a review on a few points:

  • Lightweight Process
  • Asynchronous execution
  • Easy to implement for heavy task
  • Custom Thread Management
  • Simple and quick

Custom Thread Management

Using the AsyncTask is the easiest and most convenient way to manage background tasks from within an Activity. However, in cases where tasks need to be processed in parallel with more control, or the tasks need to continue executing even when the activity leaves the screen, you’ll need to create a background service or manage threaded operations more manually.

Limitations of Android AsyncTask

The modern AsyncTask is limited to 128 concurrent tasks, with an additional queue of 10 tasks (if supporting Android 1.5, it’s a limit of ten tasks at a time, with a maximum queue of 10 tasks). That means that if you queue up more than 138 tasks before they can complete, your app will crash.

AsyncTask will not be able to update the UI on completion. If the Activity is destroyed or the configuration changes then the. As a result, for short one-off background tasks tightly coupled to updating an Activity, we should consider using an AsyncTask as outlined above.

Result Screen for Asynctask example with Progress Bar

In the given image outputs produced by the project where the time set by the user is 5 seconds.

Conclusion

Published by Amit Pandey

Professional Android app Developer, Full Stack Web Developer, Blogger, Entrepreneur And Founder of Coding Issue. I am always trying to learn new things or new possible ways to do things.

Did you find this page helpful?

X

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

*