这是来自 Android 开发者网站的示例代码:
public void onClick(View v) {
new DownloadImageTask().execute("http://example.com/image.png");
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
/** The system calls this to perform work in a worker thread and
* delivers it the parameters given to AsyncTask.execute() */
protected Bitmap doInBackground(String... urls) {
return loadImageFromNetwork(urls[0]);
}
/** The system calls this to perform work in the UI thread and delivers
* the result from doInBackground() */
protected void onPostExecute(Bitmap result) {
mImageView.setImageBitmap(result);
}
}
在这里,在行new DownloadImageTask().execute("http://example.com/image.png");
中,确实new DownloadImageTask()
创建了DownloadImageTask
类的对象,还是创建了扩展的匿名类DownloadImageTask
?
为了比较:
在这段代码中,
public void onClick(View v) {
new Thread(new Runnable() {
public void run() {
Bitmap b = loadImageFromNetwork("http://example.com/image.png");
mImageView.setImageBitmap(b);
}
}).start();
}
已创建并实例化了派生自 Thread 类的类的对象,但没有保存对它的引用(这使得它不能在将来再次使用),而不是 Thread 类型的对象。以前的代码是什么情况?