官术网_书友最值得收藏!

Executing AsyncTasks

Having implemented doInBackground and onPostExecute, we want to get our task running. There are two methods we can use for this, each offering different levels of control over the degree of concurrency with which our tasks are executed. Let's look at the simpler of the two methods first:

  public final AsyncTask<Params, Progress, Result> execute(Params...
  params)

The return type is the type of our AsyncTask subclass, which is simply for convenience so that we can use method chaining to instantiate and start a task in a single line and still record a reference to the instance:

   class MyTask implements AsyncTask<String,Void,String>{ ... }
   MyTask task = new MyTask().execute("hello");

The Params... params argument is the same Params type we used in our class declaration, because the values we supply to the execute method are later passed to our doInBackground method as its Params... params arguments. Notice that it is a varargs (variable number of parameters) parameter, meaning that we can pass any number of parameters of that type (including none).

Note

Each instance of AsyncTask is a single-use object—once we have started an AsyncTask, it can never be started again, even if we cancel it or wait for it to complete first.

This is a safety feature, designed to protect us from concurrency issues such as the race condition.

Executing DownloadImageTask is straightforward—we need Activity, which constructs an instance of DownloadImageTask with a view to update, and then we invoke the execute method with a suitable value for the URL:

  public class ShowMyPuppyActivity extends Activity {
   
    @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.show_my_puppy);

  // Get the show button reference
  Button showBut = (Button) findViewById(R.id.showImageBut);
  showBut.setOnClickListener(new View.OnClickListener() {
   
    @Override
    public void onClick(View v) {
      ...
      // My Puppie Image URL
      URL url = new URL("http://img.allw.mn/" +
                    "content/www/2009/03/april1.jpg");  
      // Get the Reference to Photo UI Image View               
      ImageView iv = (ImageView) findViewById(R.id.photo);
        // Download the Image in background and
        // load the image on the view
        new DownloadImageTask(iv).execute(url);
        ...
      }
  });
}

Once we click on the UI show button, a new DownloadAsyncTask is created and attached to an imageView and we call the execute() method to start the async task in the background. When we call the execute() method on the task, this will result in a call to the onPreExecute() method followed by a call to the doInBackground() method.

Like we explained before, once the download is finished, the onPostExecute() is called to load the image downloaded (Bitmap) on the image view.

Tip

You can download the example code files for all Packt Publishing books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

主站蜘蛛池模板: 南溪县| 武胜县| 中西区| 老河口市| 孝感市| 芦溪县| 秭归县| 和顺县| 隆化县| 金昌市| 通海县| 女性| 云阳县| 呼伦贝尔市| 江华| 龙南县| 麦盖提县| 象州县| 沁水县| 内黄县| 贞丰县| 集贤县| 元阳县| 准格尔旗| 舞阳县| 定日县| 江陵县| 合水县| 花莲市| 新巴尔虎右旗| 安宁市| 当雄县| 南木林县| 安龙县| 两当县| 皮山县| 天水市| 杂多县| 安康市| 湘阴县| 湘潭县|