Async Task and / or Application

0

I'm developing a app that needs to be always connected to the server. For this I thought to use a async task to run the connection to the server in the background but when I change from Activity the connection ends.

I've been reading some documentation on the internet and realized that I can always make the connection to the server constant through Aplication .

My problem is that I can not figure out what the difference is between Aplication and Async Task , why can the two result in the same result?

    
asked by anonymous 26.03.2014 / 13:24

1 answer

7

The Aplication class is like a singleton, it is one for each Android application. But it was not designed to perform tasks in the background, but to save runtime information about the application. As it says in javadoc:

  

Application

     

Base class to maintain application state. You can provide your own implementation by specifying in your AndroidManifest.xml's in the tag, this causes your class to be instantiated when the process of your application / package is created.    link

AsyncTask already

  

AsyncTask facilitates and enables proper use of the UI Thread (The Thread that draws on the screen). This class allows you to perform background operations and publish results to the screen without having to manipulate Threads and / or Handlers.

Soon AsyncTask does background operations to show somewhere, and Application is used to save application states.

Neither of these exactly matches your need to run an application that is always connected to the server.

For this you need to use another structure, which is Service . You can get more information about services in the official documentation:

  

link

    
26.03.2014 / 13:45