I need advice is this solution acceptable and not cause overflow, I update data which read with AsyncTask, after AsyncTask finished I need to update again and again. Is this solution acceptable and safe
private class DownloadFilesTask extends AsyncTask<URL,Integer,com.ring_view.www.json.System> {
@Override
protected com.ring_view.www.json.System doInBackground(URL... params) {
int count = params.length;
URL temp=params[0];
System system=null;
try {
system = Communicator.getSystem(temp);
} catch (LoggingConnectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONParsingErrorException e) {
// TODO Auto-generated catch block
e.printStackTrace();
开发者_Go百科 }
return system;
}
protected void onProgressUpdate(Integer... progress) {
//setProgressPercent(progress[0]);
}
protected void onPostExecute(com.ring_view.www.json.System result) {
txtWorkAllowedValue.setText(result.work_allowed);
try {
new DownloadFilesTask().execute(new URL("http://test/status-system.json"));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
I first time call new DownloadFilesTask().execute(new URL("http://test/status-system.json")); in OvCreate method and it works fine in emulator. Is this safe or there is some more elegant solution ?
Yes, instantiating an AsyncTask multiple times is acceptable, example...
new DownloadFilesTask().execute(...);
...
new DownloadFilesTask().execute(...);
...is allowed.
You must not do something like the following though...
DownloadFilesTask myTask = new DownloadFilesTask();
myTask.execute(...); // This is OK
myTask.execute(...); // This will cause an exception
This is because it isn't legal to execute the same thread twice. In the first example using new
repeatedly creates a new thread for doInBackground(...)
but in the second example it is trying to re-use the previous one.
By default AsyncTask handles it's own object pool automatically. So you dont have to worry about overflowing. I think it only allows 10 AsyncTasks to run at any one time by default, im not sure the exact number. And yes, like MisterSquonk said you have to have to create a new task each time.
You just need something to make it stop, like an index or a condition, or it will keep running forever.
I did the following:
private ProgressDialog mProgressDialog;
private class FilesDownloadTask extends AsyncTask<Integer, Integer, Integer> {
private Context context;
public FilesDownloadTask(Context context) {
this.context = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
if (mProgressDialog == null) {
mProgressDialog = new ProgressDialog(context);
mProgressDialog.setMessage(getString(R.string.downloading));
mProgressDialog.setIndeterminate(true);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
}
@Override
protected Integer doInBackground(Integer... index) {
int i = index[0];
if (i < fileList.length) {
if (!new File(path[i]).exists())
doDownload(urls[i], path[i]);
publishProgress(i);
}
return i++;
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(fileList.length);
mProgressDialog.setProgress(progress[0]);
}
@Override
protected void onPostExecute(Integer nextIndex) {
if (index < fileList.length)
new FilesDownloadTask(context).execute((Integer) nextIndex);
else
mProgressDialog.dismiss();
}
@Override
protected void onCancelled() {
mProgressDialog.dismiss();
super.onCancelled();
}
}
精彩评论