Android 需要新异步任务递归调用的建议吗
我需要的建议是,这个解决方案是否可以接受并且不会导致溢出,我更新使用AsyncTask读取的数据,在AsyncTask完成后,我需要一次又一次地更新。该解决方案是否可接受且安全Android 需要新异步任务递归调用的建议吗,android,Android,我需要的建议是,这个解决方案是否可以接受并且不会导致溢出,我更新使用AsyncTask读取的数据,在AsyncTask完成后,我需要一次又一次地更新。该解决方案是否可接受且安全 private class DownloadFilesTask extends AsyncTask<URL,Integer,com.ring_view.www.json.System> { @Override protected com.ring_view.www.json.System d
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();
}
}
}
私有类下载文件任务扩展异步任务{
@凌驾
受保护的com.ring_view.www.json.System doInBackground(URL…参数){
int count=参数长度;
URL temp=params[0];
系统=空;
试一试{
系统=通信器。获取系统(温度);
}捕获(记录连接异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(JSONPARSINGERROR异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回系统;
}
受保护的void onProgressUpdate(整数…进度){
//setProgressPercent(进度[0]);
}
受保护的void onPostExecute(com.ring\u view.www.json.System result){
txtWorkAllowedValue.setText(允许的结果工作);
试一试{
新建DownloadFilesTask().execute(新URL(“http://test/status-system.json"));
}捕获(格式错误){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
}
我第一次调用new DownloadFilesTask()http://test/status-system.json")); 在OvCreate方法中,它在emulator中运行良好。这是安全的还是有更优雅的解决方案?是的,可以多次实例化异步任务,例如
new DownloadFilesTask().execute(...);
...
new DownloadFilesTask().execute(...);
…是允许的
但是你不能做下面这样的事情
DownloadFilesTask myTask = new DownloadFilesTask();
myTask.execute(...); // This is OK
myTask.execute(...); // This will cause an exception
这是因为执行同一线程两次是不合法的。在第一个示例中,使用
new
重复为doInBackground(…)
创建一个新线程,但在第二个示例中,它尝试重新使用前一个线程。默认情况下,AsyncTask自动处理自己的对象池。所以你不必担心溢出。我认为默认情况下,它只允许在任何时间运行10个异步任务,我不确定确切的数量。是的,就像Quonk先生说的那样,你每次都必须创建一个新任务。你只需要一些东西让它停止,比如索引或条件,否则它将永远运行
我做了以下工作:
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();
}
}
private ProgressDialog mProgressDialog;
私有类FileDownloadTask扩展了AsyncTask{
私人语境;
公共文件下载任务(上下文){
this.context=上下文;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
如果(mProgressDialog==null){
mProgressDialog=新建进度对话框(上下文);
setMessage(getString(R.string.downloading));
mProgressDialog.setUndeterminate(true);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_水平);
mProgressDialog.setCancelable(假);
mProgressDialog.show();
}
}
@凌驾
受保护的整数doInBackground(整数…索引){
int i=指数[0];
if(i