- Asynchronous Android Programming(Second Edition)
- Helder Vasconcelos
- 454字
- 2021-07-14 10:43:15
Declaring AsyncTask types
AsyncTask
is a generically typed class that exposes three generic type parameters:
abstract class AsyncTask<Params, Progress, Result>
In order to use a generic type, we must provide one type argument per type parameter that was declared for the generic type.
Note
The generic type class provides a way to re-use the same generic algorithms for different input types. A generic type could have one or more type parameters.
When we declare an AsyncTask
subclass, we'll specify the types for Params, Progress, and Result; for example, if we want to pass a String
parameter to doInBackground
, report progress as a Float
, and return a Boolean
result, we would declare our AsyncTask
subclass as follows:
public class MyTask extends AsyncTask<String, Float, Boolean>
If we don't need to pass any parameters, or don't want to report progress, a good type to use for those parameters is java.lang.Void
, which signals our intent clearly, because Void
is an uninstantiable class representing the void keyword.
Only reference types can be used as type arguments of a generic type. This includes classes, interfaces, enum types, nested and inner types, and array types. Primitive types are not allowed to be used as a type argument. The next declaration is considered illegal on a generic type class definition:
// Error public class MyTask extends AsyncTask<String, float, boolean>
Let's take a look at our first example, performing an expensive image download in the background and reporting the result into the current UI:
public class DownloadImageTask extends AsyncTask<URL, Integer, Bitmap> { // Weak reference to the UI View to update private final WeakReference<ImageView> imageViewRef; public DownloadImageTask(ImageView imageView) { this.imageViewRef = new WeakReference<ImageView>(imageView); } // Retrieves the image from a URL private Bitmap downloadBitmap(URL url) { // elided for brevity ... ... } @Override protected Bitmap doInBackground(URL... params) { URL url = params[0]; // The IO operation invoked will take a significant ammount // to complete return downloadBitmap(url); } ... @Override protected void onPostExecute(Bitmap bitmap) { ImageView imageView = this.imageViewRef.get(); if (imageView != null) { imageView.setImageBitmap(bitmap); } } }
Here, DownloadImageTask
extends AsyncTask
, specifying the Params type as a URL so that we can retrieve an image based on its url, Progress as Integer, and the Result type as Bitmap.
We pass ImageView
to the constructor so that DownloadImageTask
has a weak reference to the user interface that it should update upon completion.
We've implemented doInBackground
to download the image in the background, where url is a URL parameter with the image resource location.
In onPostExecute
, when the view weak reference is not null, we simply load the bitmap into the view that we stored in the constructor.
The WeakReference
does not prevent the view from being garbage collected when the activity where the view was created is no longer active.
- Instant Node Package Manager
- Beginning Java Data Structures and Algorithms
- Debian 7:System Administration Best Practices
- 樂(lè)學(xué)Web編程:網(wǎng)站制作不神秘
- C語(yǔ)言程序設(shè)計(jì)實(shí)訓(xùn)教程
- Learning ELK Stack
- iOS應(yīng)用逆向工程(第2版)
- FLL+WRO樂(lè)高機(jī)器人競(jìng)賽教程:機(jī)械、巡線(xiàn)與PID
- 3D少兒游戲編程(原書(shū)第2版)
- 微信小程序入門(mén)指南
- The DevOps 2.5 Toolkit
- Terraform:多云、混合云環(huán)境下實(shí)現(xiàn)基礎(chǔ)設(shè)施即代碼(第2版)
- SSM開(kāi)發(fā)實(shí)戰(zhàn)教程(Spring+Spring MVC+MyBatis)
- Troubleshooting Citrix XenApp?
- Delphi開(kāi)發(fā)典型模塊大全(修訂版)