g4gaj
g4gaj

Reputation: 85

Use AsyncTask or Service to download video from server

I want to download a video (mpeg) from my server and need to save it on the SD card. Is it bettet to use an AsyncTask or a Service?

Could you give me a concept of when to use which?

Upvotes: 0

Views: 1463

Answers (4)

David Medenjak
David Medenjak

Reputation: 34532

Android provides a DownloadManager class which will handle background downloads with minimal effort.

dm = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
final DownloadManager.Request request = new DownloadManager.Request(Uri.parse(uri))
        .setDestinationInExternalFilesDir(getContext(), "downloads", "myVideo.avi");
int downloadId = dm.enqueue(request);

By persisting downloadId it lets you cancel the download and it will also notify you upon completion with a broadcast. If needed, you can even query for progress.

Have a look at the documentation here.

Upvotes: 2

Mahesh Giri
Mahesh Giri

Reputation: 1840

That depends on your need like
Async
1. task is used to run when button is clicked.
2. Async task used for one time use.
Services
1. You may use Service if you want to download continuously from server like download images whenever new came in server.
2. Services executes in background continuously.

It is faster to use Async task if you have to done task once per click or other on events.

Upvotes: 0

lavan
lavan

Reputation: 81

It actually depends on your need.

if your download needs to update any UI/UX content after successful downloading then please go ahead to use AsyncTask.

If that is not your case then please use the Service class by launching a separate Thread process inside it or you can also use IntentService.

Upvotes: 1

Ankit Aggarwal
Ankit Aggarwal

Reputation: 5375

(1) If you just have to download it and save it to sd card and no other thing to do with it or if it is a very large video file use and doesn't require frequent interaction with UI thread Intent Service

(2) If it is a very small video file, use async task.

(3) else if it is not too large use normal service

Upvotes: 1

Related Questions