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

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.

主站蜘蛛池模板: 阿拉善左旗| 顺昌县| 偃师市| 成安县| 三原县| 金坛市| 长泰县| 宜兰市| 马山县| 栖霞市| 小金县| 永仁县| 南城县| 即墨市| 定兴县| 浦东新区| 弋阳县| 彭州市| 忻州市| 屯门区| 东宁县| 安西县| 和龙市| 义马市| 来宾市| 旬阳县| 张北县| 贵定县| 宝清县| 分宜县| 平遥县| 旅游| 保山市| 南溪县| 田阳县| 马尔康县| 利津县| 宁河县| 定日县| 新宁县| 昌图县|