AsyncTask里面的Android AsyncTask [英] Android AsyncTask inside AsyncTask

查看:25
本文介绍了AsyncTask里面的Android AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在研究条形码解码器,一旦我们将条形码转到互联网上的多个 API 以解码刚刚扫描的内容.问题是我必须将一些 XML 解析链接在一起,我不知道我是否做得对.

因此,一旦条形码被扫描,我的程序就会调用一个 ASyncTask,它通过 API 来检索产品名称.一旦有了名称,我希望它调用另一个 ASyncTask.我知道这可以通过在另一个的 onPostExecute() 中实例化 ASyncTaks 来实现,但是,我认为这是错误的,因为它就像盒子中的盒子.那么在我的主 Activity 中实例化我的第二个 ASyncTask 并让它等到我的第一个 ASyncTask 完成是否可能/更好?

(英语不是我的主要语言,我希望我说清楚了).

解决方案

我认为在第一个 AsyncTask, Mixing 的 onPostExecute 中启动第二个 AsyncTask 是绝对合法的这两种操作都是一个糟糕的逻辑想法,正如后代"所说 - 你必须将它们分开"

如果您不希望它直接位于 onPostExecute 本身内部,请设置一个处理程序以在 Activity 中执行它并从 onPostExecute 调用此处理程序.>

最后一件事 - 如果你有很多逻辑 - 将它移到一个单独的文件中,不要将它们全部保存在同一个文件中.

So, I'm working on a barcode decoder, which once we have the barcode goes to multiples API over the internet to decode what was just scanned. The thing is that I have to link some XML parsing together, and I don't know if I'm doing it right.

So, once the barcode is scanned, my program calls an ASyncTask which goes over an API to retrieve the product name. Once it has the name, I want it to call another ASyncTask. I know this is possible by instantiating an ASyncTaks in the onPostExecute() of the other but, I think this is wrong, because it's like boxes within boxes. So isn't it possible/better to instantiate my second ASyncTask inside my main Activity, and make it wait until my first ASyncTask is finished ?

(english isn't my primary language, I hope I made myself clear).

解决方案

I think it's absolutely legitimate to start the second AsyncTask in the onPostExecute of the first AsyncTask, Mixing both operations is a bad logical idea, As "The Offspring" said - "You've gotta keep 'em separated"

If you don't want it to be directly inside the onPostExecute itself, set a handler to execute it in the activity and call this handler from onPostExecute.

And last thing - If you have a lot of logic - move it to a separate file, don't keep it all at the same file.

这篇关于AsyncTask里面的Android AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆