- Asynchronous Android Programming(Second Edition)
- Helder Vasconcelos
- 455字
- 2021-07-14 10:43:15
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.
- Node.js Design Patterns
- Python科學計算(第2版)
- PostgreSQL for Data Architects
- Android 7編程入門經典:使用Android Studio 2(第4版)
- Web Application Development with R Using Shiny(Second Edition)
- JavaScript前端開發與實例教程(微課視頻版)
- 人人都是網站分析師:從分析師的視角理解網站和解讀數據
- Java 9模塊化開發:核心原則與實踐
- Python深度學習:基于TensorFlow
- Nginx Lua開發實戰
- Android玩家必備
- 持續輕量級Java EE開發:編寫可測試的代碼
- Machine Learning With Go
- Data Science Algorithms in a Week
- Mastering OAuth 2.0