某种异步任务队列
你好 我有带有文件列表的 ListView 。我单击项目并开始在 asynctask 中下载该文件。 然后我单击另一个文件,它必须放入队列中,等待该文件并在完成后开始下载。我可以创建一些类来保存所有点击的链接,并将其传递给 asynctask 下载部分?并以某种方式处理它们。但想知道这是正确的方法吗? 有建议的链接吗?谢谢
Hello
I have ListView with list of files. i click item and start to download this file in asynctask.
then i click another one and it must be put in queue, wait for that file and start ot download after it finishes. i can make some class that will hold all clicked links, and pass it to asynctask downloading part? and than somehow process them. but want to know is it the right way?
any links of sugestions? thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您决定使用 AsyncTask,那么,是的,按住单击的链接并在适当的时候启动新任务。您应该注意到,AsyncTask 就像 Android 应用程序中线程处理的“小刀”。
如果您确实需要管理一堆后台任务(听起来确实如此),请看看 ThreadPoolExecutor。你可以获得很大的灵活性。
BlockingQueue
ThreadPoolExecutor
更多信息
示例
If you're set on using AsyncTask then, yeah, hold your clicked links and kick off new tasks when appropriate. You should note that AsyncTask is like the 'pocket knife' for threading in Android apps.
If you really need to manage a bunch of background tasks, and it sounds like you do, take a look at ThreadPoolExecutor. You get a lot of flexibility.
BlockingQueue
ThreadPoolExecutor
More Info
Example
看一下 HandlerThread 和 Handler 类。您需要一个处理程序将任务传递到后台 HandlerThread,另一个处理程序供 UI 线程将结果传递回 UI
Take a look at HandlerThread and the Handler class. You need one handler to pass tasks to the background HandlerThread and another for the UI thread to pass results back to the UI
尽管很老,但还是从 Google 得到的:考虑一下 IntentService。
Even though old, got here from Google: consider an IntentService.