I have an android app, written in java which uses AsyncTask to download data. The AsyncTask is needed to handle the progress dialog box which is shown throughout the download.
The actual download is initiated through a separate thread, downloadAllUpdater. Whilst I am aware this is not convention, a thread is required as the process of downloading is spread across 7 classes and there was no other way to update the progress bar whilst the download was occurring as I couldn't run the publishProgress call from any other class.
My main activity is shown below:
private Download downloadAll;
private int totalBlocksLeft;
private boolean downloadComplete;
private boolean downloadFailed;
private PerformTask currentPerformTask;
public void downloadAllClick(View view) //When the download all button is clicked
{
currentPerformTask = new PerformTask();
currentPerformTask.execute();
}
private class PerformTask extends AsyncTask<Void, Integer, Integer>
{
protected void onPreExecute()
{
usingDialog = new ProgressDialog(WifiAudioActivity.this);
usingDialog.show();
}
protected Integer doInBackground(Void... voi)
{
downloadAll = new Download();
downloadComplete = false; //Assume the download is not complete
downloadAllUpdater.start();
downloadFailed = false;
while ((downloadComplete == false) && (downloadFailed == false))
{
blocksDownloaded = downloadAll.getTotalBlocksLeft();
publishProgress(blocksDownloaded);
}
return 0;
}
protected void onProgressUpdate(Integer... progress)
{
usingDialog.setProgress(progress[0]);
}
protected void onPostExecute(Integer result)
{
usingDialog.dismiss();
}
}
Thread downloadAllUpdater = new Thread()
{
public void run()
{
int runRetryCount = 0;
while ((!(downloadComplete)) && (!(downloadFailed)))
{
downloadComplete = download.downloadAudio(totalBlocksLeft); //Download the audio
if (!(downloadComplete))
{
runRetryCount++;
}
if (runRetryCount > Consts.RETRY_TOTAL)
{
downloadFailed = true;
}
}
}
};
The download is initiated with the click of the button, which initiates the downloadAllClick() method.
This all works fine the first time the button is pressed.
However, when the button is pressed a second time (after the first time completition), I receive an error and the program force closes.
I am pretty sure the error is because I am running the Thread, downloadAllUpdater a second time, because if I have a separate method with just downloadAllUpdater.start(), it also crashes with the same error.
Can anyone help? Below is the error log:
08-23 11:22:34.954: W/dalvikvm(1485): threadid=14: thread exiting with uncaught exception (group=0x400259f8)
08-23 11:22:35.014: E/AndroidRuntime(1485): FATAL EXCEPTION: AsyncTask #5
08-23 11:22:35.014: E/AndroidRuntime(1485): java.lang.RuntimeException: An error occured while executing doInBackground()
08-23 11:22:35.014: E/AndroidRuntime(1485): at android.os.AsyncTask$3.done(AsyncTask.java:200)
08-23 11:22:35.014: E/AndroidRuntime(1485): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-23 11:22:35.014: E/AndroidRuntime(1485): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-23 11:22:35.014: E/AndroidRuntime(1485): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-23 11:22:35.014: E/AndroidRuntime(1485): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-23 11:22:35.014: E/AndroidRuntime(1485): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
08-23 11:22:35.014: E/AndroidRuntime(1485): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
08-23 11:22:35.014: E/AndroidRuntime(1485): at java.lang.Thread.run(Thread.java:1102)
08-23 11:22:35.014: E/AndroidRuntime(1485): Caused by: java.lang.IllegalThreadStateException: Thread already started.
08-23 11:22:35.014: E/AndroidRuntime(1485): at java.lang.Thread.start(Thread.java:1331)
08-23 11:22:35.014: E/AndroidRuntime(1485): at com.que.wifiaudio.WifiAudioActivity$PerformTask.doInBackground(WifiAudioActivity.java:518)
08-23 11:22:35.014: E/AndroidRuntime(1485): at com.que.wifiaudio.WifiAudioActivity$PerformTask.doInBackground(WifiAudioActivity.java:1)
08-23 11:22:35.014: E/AndroidRuntime(1485): at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-23 11:22:35.014: E/AndroidRuntime(1485): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-23 11:22:35.014: E/AndroidRuntime(1485): ... 4 more