onPreExecute(): before the task is executed, Main
doInBackground(Params…): After onPreExecute, Background
onProgressUpdate(Progress…): After publishProgress() is called, while doInBackground is executing, Main
OnPostExecute(Result): After doInBackground() finishes, Main
Review of generics
ArrayList
add(E e)->requires object of type E as input
get(int index)->returns object of type E
ArrayAdapter
private class DownloadFileTask extends AsyncTask{
protected Long doInBackground(RUL... urls){
int count = urls.length;
long totalSize = 0;
for(int i = 0; i < count; i++){
totalSize += Downloader.downloadFile(url[i]);
publishProgress((int)((i/(float) count)* 100));
if(isCancelled()) break;
}
return totalSize;
}
protected void onProgressUpdate(Integer... progress){
setProgressPercent(progress[0]);
}
protected void onPostExecute(long result){
showDialog("Downloaded " + result + " bytes");
}
}
private class DownloadWebpageTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... urls){
try {
return downloadUrl(urls[0]);
} catch (IOException e){
return "Unable to retrieve web page. URL may be invalid.";
}
}
@Override
protected void onPostExecute(String result){
textView.setText(result);
}
}