Tuesday, March 19, 2013

Android AsyncTasks (MultiThreading)

This tutorial explains what AsyncTasks are in Android, why you should use them, and demonstrates a simple app that calculates prime numbers to show them in action.

1. What is an AsyncTask and Why Should I use Them?

AsyncTasks are asynchronous because they operate in a thread separate from the UI thread.

You should use them to complete performance intensive tasks or tasks that may complete in an indefinite amount of time. If you do not, the user interface will be blocked until the task is finished.

For example: if you trying to download a huge web page, if you task is run in the UI thread you will prevent the application from respond to other actions--such as pressing other buttons, zooming, pressing the back button--and your app will be frozen.

No one likes it when an app completely freezes, even if it is doing legitimate loading/calculating work. Additionally, if your app blocks the UI thread for more than 20 or so seconds, the Android Operating System will assume it is broken, and a force-close dialog will appear.



2. Using AsyncTasks

It is fairly easy to use AsyncTasks. You just need to extend the AsynTask class and choose three generic types to represent the parameters, the progress, and the result.

To demonstrate AsyncTasks we will create a simple task that just sleeps for 20 seconds to simulate an actual task. Listed below is the code that defines our task.


private class ExampleAsyncTask extends AsyncTask<String, Integer, Integer> {
 
 @Override
 protected void onPreExecute() {
  super.onPreExecute();
 }

 @Override
 protected Integer doInBackground(String... params) {
  
  try {
   Thread.sleep(5000);
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
  
  return 0;
 }
 
 @Override
 protected void onPostExecute(Integer result) {
  super.onPostExecute(result);
  
  displayText.setText("Done!");
 }
 
}


The only method that you are required to implement is the doInBackground method. Since this method will run in the background it is important not to update any UI elements in it.

There are a couple other methods that you can implement including onPreExecute and onPostExecute. These methods are self-explanatory about when they run. They run on the UI thread, so it is okay to update UI elements from within these methods.

3. Starting the AsyncTask

Listed below is a code snippet to start our ExampleAsyncTask


ExampleAsyncTask task = new ExampleAsyncTask();
task.execute((String[])null);


4. Full Example Application

Listed below is the code for a full example application.

This application consists of a text view and a button. When the button is clicked an AsyncTask starts that sleeps for five seconds before updating the text view to display "Done."

Screenshot of the Example Application


MainActivity.java

package com.androiddom.async;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
 
 private TextView displayText;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        displayText = (TextView) findViewById(R.id.textView1);
        
        Button startAsyncTaskButton = (Button) findViewById(R.id.button1);
        startAsyncTaskButton.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    ExampleAsyncTask task = new ExampleAsyncTask();
    task.execute((String[])null);
   }
  });
    }
    
    private class ExampleAsyncTask extends AsyncTask<String, Integer, Integer> {
     
     @Override
     protected void onPreExecute() {
      super.onPreExecute();
     }

  @Override
  protected Integer doInBackground(String... params) {
   
   try {
    Thread.sleep(5000);
   } catch (InterruptedException e) {
    e.printStackTrace();
   }
   
   return 0;
  }
  
  @Override
  protected void onPostExecute(Integer result) {
   super.onPostExecute(result);
   
   displayText.setText("Done!");
  }
     
    }
}


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="5dp">
    <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="AsyncTask Example" android:textSize="30dp"></TextView>
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start Async Task" android:layout_marginTop="10dp"></Button>
</LinearLayout>


No comments:

Post a Comment