I want the AsyncTask to continue put data in my server in the background after the user closes the app.
The purpose of AsyncTask
is NOT to perform background tasks that's beyond the scope of an Activity
's lifecycle. When a background thread is assigned from a thread pool, it expects to return it once the task is over. ie you cannot use an AsyncTask
for which the required time is indefinite.
What you are looking for is Services in Android .
Why even use AsyncTask if we have services?
Well, say for instance you require to download an image/song from an Activity
on click of a Button
or you need to perform a task that would take some time to finish its execution. Performing these tasks from the Main thread(aka UI thread) is a bad approach and would make your app sluggish and eventually can lead to an ANR. So these tasks are to be processed asynchronously from a separate thread to keep the app butter smooth.