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

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.

主站蜘蛛池模板: 从江县| 额尔古纳市| 盖州市| 垫江县| 张掖市| 哈尔滨市| 红安县| 师宗县| 台东县| 卢龙县| 介休市| 邢台县| 五大连池市| 宣武区| 汶上县| 清镇市| 扬中市| 阿尔山市| 惠州市| 资兴市| 竹溪县| 芜湖县| 洮南市| 澎湖县| 泸溪县| 建宁县| 宝山区| 壤塘县| 黑河市| 西安市| 泰宁县| 垦利县| 湟中县| 长寿区| 阿尔山市| 清镇市| 洛浦县| 舟山市| 攀枝花市| 苏尼特右旗| 清丰县|