Question

I upload files to my server using asynctask method in android.It works fine. But when a file is big and uploading it to server takes long time ,if I exit the app while the process is continuing, at this time I do not want to block my process , I want to upload file to server even I closed the app. Which method I should use?

Was it helpful?

Solution

Use a Service to do the upload. When the user decides to upload a file instead of starting a thread or an AsyncTask from the activity itself, start a service and do the upload from there. This way even if the activity gets destroyed the service will still run and should finish the file upload.

Then you can do some check if the when the upload is over the activity is still "alive" you can show whatever you need to show there.

Check out this: http://developer.android.com/guide/components/services.html

NOTES:

  1. Services are not run on a separate thread, so long running operations like network uploads should still be done using thread or AsyncTask.

  2. Services like activities may be killed by the system. It might be the case that your entire process will be killed. Start the service with the START_STICKY flag to notify the system that if the service gets killed before it finished it's job, the system should restart the service as soon as it can.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top