从服务(IntentService)和活动执行的AsyncTask - 是有区别吗? [英] AsyncTask executed from Service (IntentService) and Activity - is there a difference?

查看:272
本文介绍了从服务(IntentService)和活动执行的AsyncTask - 是有区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有AsyncSync任何区别正在从活动或IntentService解雇了?

Is there any difference between AsyncSync being fired up from Activity or IntentService?

我这下载和上传文件通过HTTP构建应用程序。我使用自定义通知的布局,为每个传输进度条。
我在做并行传输或将它们放入队列(你会推荐哪个选项?)之间进行选择。

I'm building an app which downloads and uploads files via http. I use custom notification layout with progress bar for each transfer. I choose between doing transfers in parallel or putting them into queue (which option would you recommend?).

对于队列中的选项我使用IntentService,因此Android框架负责把任务分解成一个队列我。
对于并行有他们,我用AsyncTasks。但我解雇他们从IntentService(可能是服务以及) - 是否有这样做的呢? IntentService是正确的它执行的AsyncTask后终止,所以AsyncTask的运行没有任何父。

For the option with queue I use an IntentService, so Android framework takes care of putting tasks into a queue for me. For having them in parallel I use AsyncTasks. But I fire them up from IntentService (could be Service as well) - is there any point doing so? IntentService is terminated right after it executes AsyncTask, so the AsyncTask runs without any "parent".

如果我解雇了从活动的人的AsyncTask,转到主屏幕和系统决定关闭此活动?这能做到吗?请问AsyncTask的生存呢?

What if I fired up those AsyncTask from Activity, go to the homescreen and system decided to close this Activity? Can it do that? Will the AsyncTask survive it?

什么是这种情况的preferred的方法呢?

What would be the preferred approach for this case?

推荐答案

AsyncTask的是不是真的适合东西的地方你担心一个组件的生命周期之外存活(如果你需要的信息返回到该组件)。如果你打算有一个服务的程度,我甚至不会用AsyncTasks打扰。至于平行或队列,真的取决于很多不同的变量,但我肯定不会让任何数量的下载/上传的完全平行。我设置并发传输的最大数量限制的一些

AsyncTask isn't really appropriate for things where you're concerned about surviving outside the lifecycle of a component (if you need to message back to that component). If you're going to the extent of having a service, I wouldn't even bother with AsyncTasks. As far as parallel or queue, that really depends on a lot of different variables, but I certainly wouldn't make it completely parallel for any number of downloads/uploads. I'd set some limit on the max number of concurrent transfers.

有在使用IntentService并行exeuction是没有意义的 - ......你已经注意到这个问题。你进入领土上​​是不是真的被Android API覆盖。 AsyncTask的和IntentService是很好的抽象,使几种情况容易,但很多很多任务并行执行是不是其中之一。这也可能是最好使用一些Java线程/并发类。看看的ThreadPoolExecutor

There's no point in using IntentService for parallel exeuction--you've noted the problem with that already. You're getting into territory which is not really covered by the Android API. AsyncTask and IntentService are nice abstractions that make several scenarios easy, but parallel execution of many many tasks is not one of them. It's probably best to use some of the Java threading/concurrency classes. Take a look at ThreadPoolExecutor.

这篇关于从服务(IntentService)和活动执行的AsyncTask - 是有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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