realtebo
realtebo

Reputation: 25661

Android: download from web in background

I've the need to create a simple app that do this: - every run try to download somewhat like 20 pdf from web (every time from the same location, because the server keep it updated every minute) - the main activity will be only a list of pdf. One dir will be scanned for downloaded files. If there are any, then them will be listed.

The problem is I need to download 'in background', but not with a 'never-ending service'.

I'm thinking to create launch a sevice when application start. The main activty scan the dir e show the pdf, while the service try to download the pdf.

If the first time not all of the pdf are yet downloaded, is not a problem.

I tried to do this, but the service 'lock' the app, so the app is not responding.

I was thinking that service run into a separate thread. Is is real ?

So: wha't the best solution to have the activities running smooth, but in background 'something' download a lot of file ?

Upvotes: 0

Views: 1106

Answers (2)

AlexN
AlexN

Reputation: 2554

I would recommend to deeply check Intent Service conception. http://developer.android.com/reference/android/app/IntentService.html

From one side, it's a service. That means that system will not probably kill the downloading when user will press "Home" from your app. However, it is not

never-ending service

As it will be finished exactly after downloading complete (If no more task will be launched).

And answering your question:

I was thinking that service run into a separate thread. Is is real ?

This is not 100% true. If you just use startService and do a heavy task in Service.onCreate() - than it will hang your application, as it's running in UI Main thread. However, if you'll extends IntentService - it has a convenient method to override onHandleIntent ( http://developer.android.com/reference/android/app/IntentService.html#onHandleIntent(android.content.Intent )), which will be executed in separate thread. IntentService will do that for you.

Let me know if any questions.

Upvotes: 0

user370305
user370305

Reputation: 109237

Android-AsyncTask or Android-IntentService is best suited for your requirements.

If you will going to use AsyncTask you can update your Activity UI periodically while downloading continues.

Upvotes: 3

Related Questions