Downloading an image from Android: using an activity or service?

I am currently using Activity to upload images to the Internet, I am using AsyncTask, and everything works fine. I am currently using Activity to achieve this, basically it allows the user to enter a few details, take a picture and then send until a response is received. When on compounds E or G the whole process can be quite long (in excess of a minute). It is not possible to complete the work and return to the previous action (this is what I want) until a response is received.

What options do I have? Currently, if I press the home button, the process is working fine and continues to load, can the application be minimized by programmability ?, I see a lot of problems with this approach, for example, if the activity is closed or an error occurs.

The only approach I can come up with is to transfer the actual download code to the service, collect information using activity, and allow activity to launch a new service for each download? I can then notify the user of success or failure using the NotificationManager and process the retry in the service.

I noticed that when sharing the image from the gallery on Facebook, the action closes immediately, and the user receives feedback through a notification. I assume this approach uses a service to upload images to Facebook?

Any help / advice would be greatly appreciated.

Hi

+7
source share
1 answer

I suggest you use the following setting:

  • Use your activity to prepare (configure / configure) what the user wants to do (for example, select the image he wants to upload and the destination folder)
  • When the user clicks Submit, call the service that will perform the download task. I will note some of the benefits of this later)
  • At the end of the service, create a notification to display the results. You should use OnGoingNotification so that the user knows that the file is loading (it may also have a progress indicator)

Using a service instead of an Activity has some advantages. For example, you do not need to worry about configuration changes (for example, about changing the orientation of the device), you do not "block" the user in action, where he has nothing to do, etc.

Downloading does not require a user interface, so using activity is not the best way to do this.

In short: use the service

+14
source

All Articles