Quantcast
Channel: makemachine » Android
Viewing all articles
Browse latest Browse all 5

Android AsyncTask Example

$
0
0

Today I was working on a loading screen for an Android app. The purpose of the loading screen is to report to the user which boot up processes were happening as well as provide a nice looking intro screen for the app. The U.I. layer of an Android app runs in a single thread. You can think of a thread as a stack of function calls. Each function in the list executes only after the one before it has completed. This is a very simplistic view of a thread but for the sake of this post it will work. With multiple processor intensive functions stacked up the U.I. layer can become inactive and appear to hang while waiting for the stack to finish. This problem can be solved by splitting up the processes into separate threads. By doing so your U.I. layer can update concurrent with the other more intensive processes.

The Android operating system provides a couple of solutions to this problem. The solution I implemented for the loading screen was a class called AsyncTask. It’s a wrapper for a threaded operation that provides some really convenient callbacks for updating progress information and handling task completion.

» Download complete example code here

Let’s look at how to go about instantiating an AsyncTask. First off, you must sub-class AsyncTask and provide three parameters. These three parameters indicate the types of objects that are passed to three callbacks you can override in your sub-class. You can set these parameter to any type that suits your application. In the following example I am using a Context object and two Integer objects.

protected class InitTask extends AsyncTask<Context, Integer, Integer>
In the line above we define our sub-class and the three parameters that will be passed to the callbacks. The callbacks look like this:

doInBackground()

@Override
protected Integer doInBackground( Context... params )  {
     return super.doInBackground( params )
}

Anything processed in this method is handled in a sperate thread. Note that the data type of the return value is an Integer and corresponds to the type third parameter in the class definition. This value returned from this method is passed to the onPostExecute() method when this thread completes.

onProgressUpdate()

@Override
protected void onProgressUpdate(Integer... values)  {
     super.onProgressUpdate(values);
}

Report progress of async operations via this method. Note the param with datatype Integer. This corresponds to the second parameter in the class definition. This callback can be triggered from within the body of the doInBackground() method by calling publishProgress()

onPostExecute()

@Override
protected void onPostExecute( Integer result )  {
      super.onPostExecute(result);
}

This method is called when the thread has completed successfully. Note that the datatype passed to this parameter matches the third parameter in the method definition.

The AsyncTask class provides to other helpful callback methods onCancelled() which handles the cancellation of the thread and onPreExecute() which is called just before the thread calls doInBackground().

A complete example project using AsyncTask is available from the makemachine code repository.


Viewing all articles
Browse latest Browse all 5

Latest Images

Trending Articles





Latest Images