需要咨询新的AsyncTask递归调用 [英] Need advice new AsyncTask recursive calling
问题描述
我需要的建议是接受的解决方案,并不会造成溢出,我更新它与AsyncTask的阅读,之后的AsyncTask完成我需要一次又一次的更新数据。这是接受的解决方案和安全
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();
}
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();
}
}
}
我第一次调用新DownloadFilesTask()执行(新URL(HTTP://test/status-system.json));在OvCreate方法,它工作在模拟器罚款。这是安全的或有一些更优雅的解决方案?
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 ?
推荐答案
是的,实例化一个AsyncTask的多次是可以接受的,例如...
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
这是因为它是不合法的执行两次相同的线程。在第一个例子中使用新
多次创造了一个新的线程 doInBackground(...)
但在第二个例子中它正在试图重新使用previous之一。
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.
这篇关于需要咨询新的AsyncTask递归调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!