需要建议新的AsyncTask递归调用(Need advice new AsyncTask recursive calling)

编程入门 行业动态 更新时间:2024-10-26 06:30:38
需要建议新的AsyncTask递归调用(Need advice new AsyncTask recursive calling)

我需要建议是这个解决方案可以接受并且不会导致溢出,我更新了使用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(); } } }

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

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

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是可以接受的,例如......

new DownloadFilesTask().execute(...); ... new DownloadFilesTask().execute(...);

...被允许。

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

DownloadFilesTask myTask = new DownloadFilesTask(); myTask.execute(...); // This is OK myTask.execute(...); // This will cause an exception

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

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.

更多推荐

本文发布于:2023-08-01 21:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1365043.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   建议   AsyncTask   calling   advice

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!