需要建议新的AsyncTask递归调用

Dam*_*mir 3 android

我需要建议是这个解决方案可以接受并且不会导致溢出,我更新了使用AsyncTask读取的数据,在AsyncTask完成后我需要一次又一次地更新.这个解决方案是否可接受且安全

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();
        }
     }

 }
Run Code Online (Sandbox Code Playgroud)

我第一次调用new DownloadFilesTask().execute(new URL("http://test/status-system.json")); 在OvCreate方法中,它在模拟器中工作正常.这是安全还是有一些更优雅的解决方案?

Squ*_*onk 5

是的,多次实例化AsyncTask是可以接受的,例如......

new DownloadFilesTask().execute(...);
...
new DownloadFilesTask().execute(...);
Run Code Online (Sandbox Code Playgroud)

...被允许.

你不能做类似以下的事情......

DownloadFilesTask myTask = new DownloadFilesTask();
myTask.execute(...); // This is OK
myTask.execute(...); // This will cause an exception
Run Code Online (Sandbox Code Playgroud)

这是因为两次执行相同的线程是不合法的.在第一个示例中,使用new重复创建新线程,doInBackground(...)但在第二个示例中,它尝试重新使用前一个线程.