Is it better to use AsyncTask or Service to upload

2019-02-22 02:25发布

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 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 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.

1条回答
做自己的国王
2楼-- · 2019-02-22 03:13

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.

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.

查看更多
登录 后发表回答