它是更好地使用的AsyncTask或服务上载的背景文件? [英] Is it better to use AsyncTask or Service to upload a file in background?

查看:78
本文介绍了它是更好地使用的AsyncTask或服务上载的背景文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,其中用户能够将视频上传到亚马逊的S3。我已经使用亚马逊SDK java的高级API来实现这一点。在上载过程中,如果用户点击home键上传必须继续在后台运行。

I have a requirement where a user is able to upload a video to Amazon S3. I have achieved this using the java high-level api in amazon sdk. During the upload process if the user clicks the home button the upload must continue in the background.

什么将是一个更好的方法:

What would be a better approach :?

* 1使用的AsyncTask:我已经尝试使用AsyncTask的尝试,它工作正常。但是,如果上传过程持续较长的时间,在此背景下,OS杀死应用程序来释放内存。有什么办法,我可以处理这种情况,并让我的应用程序完成上传过程。

*1 Using AsyncTask: I have tried using AsyncTask and it works fine. But if the uploading process continues for long interval at the background, the OS kills the app to free memory. Is there any way that i can handle this situation, and let my app complete the upload process.

* 2使用服务:有人建议我使用一个服务+ UI通知。我觉得用AsyncTask的,因为它为我工作正常。有没有使用服务过的AsyncTask的任何优势。

*2 Using Service: Someone suggested me to use a Service + ui notification. I feel like using AsyncTask, because it works fine for me. Is there any advantage of using a Service over AsyncTask.

推荐答案

在大多数情况下的AsyncTask的连接到你的用户界面,启动它等。这些将留在内存中,直到任务完成的活动。

Most of the time an AsyncTask is coupled to your UI, the Activity that started it etc. Those will stay in memory until the task is finished.

这上传你的情况乞求通过 IntentService 实施。这将脱钩上传从一个特定的活动,让你的应用程序的好公民的Andr​​oid关于Android的生命周期。

This upload scenario of yours begs for implementation through an IntentService. This will decouple the uploading from a specific activity and make your App a good Android citizen in regard to the Android life cycle.

您现在可以创建一个周期性的,显示上传的状态,并允许用户取消上传从他的状态栏上的服务更新的通知。

You can now create a Notification that is periodically updated from the Service that shows the status of the upload and lets the user cancel the upload from his status bar.

这篇关于它是更好地使用的AsyncTask或服务上载的背景文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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