1

This is in my main activity:

   @Override
public void onBackPressed() {

    super.onBackPressed();
    finish();
    moveTaskToBack(true);
    System.exit(0);
}

I want the AsyncTask to continue put data in my server in the background after the user closes the app.

grantespo
  • 2,233
  • 2
  • 24
  • 62

3 Answers3

2

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.

Community
  • 1
  • 1
OBX
  • 6,044
  • 7
  • 33
  • 77
  • I have never heard of services until now. Definitely will check it out. – grantespo Feb 04 '17 at 17:52
  • That's the best part of being a part of community. You learn new things and share new things :) – OBX Feb 04 '17 at 17:53
  • Why even use AsyncTask if we have services? – grantespo Feb 04 '17 at 17:57
  • Does `System.exit(0);` finish a service? – grantespo Feb 04 '17 at 23:41
  • @OBX You're assumption that the AsyncTask is an inner class of an activity is a big assumption. AsyncTasks don't have to have a reference to a UI component, and thus can have a lifetime longer than the activity (in your example). Unless it's an intent service, all services run on the main thread, in which case you still need to move the work to a background thread. AsyncTasks are an efficient way to do that, since they take threads from a thread pool. An Intent Service is another good approach. Which way you go depends a lot on the specifics of the kind of work you're doing. – dhaag23 Feb 06 '17 at 04:30
1

Services is one part of a solution, but note that a service runs on the foreground thread. You would want to run your AsyncTask from the service to ensure that it continues to run on a background thread.

Personally, I would recommend against using a service in favor of the JobScheduler or if you need to support devices below API 21, Evernote's JobManager (which is also a bit easier to use). These will help you schedule your background operations at appropriate times, to minimize battery use or when the device is idle. It's important to be a good citizen when using the device's resource.

dhaag23
  • 6,106
  • 37
  • 35
  • Does `System.exit(0);` finish a service? – grantespo Feb 04 '17 at 23:42
  • @grant System.exit(0) will stop the service. It essentially crashes the app process. However, the service might be restarted depending on the type of service you've created. If the service is what's known as a Sticky Service (https://developer.android.com/reference/android/app/Service.html#START_STICKY) then the OS will restart your service, which of course restarts the process. – dhaag23 Feb 06 '17 at 04:22
0

You have to check Services or/and IntentServices

oskarko
  • 3,382
  • 1
  • 26
  • 26