Async Task and/or Application

Asked

Viewed 231 times

0

I’m developing a app that you need to always be 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 switch to Activity the link ends.

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

My problem is I can’t understand the difference between Aplication and Async Task, why both can give rise to the same result ?

  • As far as I know, the class Application represents your application and does not offer any feature to maintain connection with a server. You do not mean Service?

  • Supposedly we can have several 'Application' in our App. I did not understand very well this concept applied to programming in Android is why my doubt lies in Application or Async Task.

  • Incorrect. Where you read this? Application should be one per app (the AndroidManifest.xml only allows you to register an application subclass in the attribute android:name of the element <application>, when that subclass exists). Again, you don’t want to say Service?

  • I use and recommend the Volley library that is recommended by Google. https://developer.android.com/training/volley/simple.html I make a single instance of it as an Application. From a research on.

1 answer

7


The class Aplication is like a Singleton, is one for each application Android. But it was not designed to perform background tasks, but rather to store information on the application. As it says in the javadoc:

Application

Base class to maintain application status. You can provide your own implementation by specifying your Androidmanifest.xml’s in the tag, this causes your class to be instantiated when your application/package process is created. http://developer.android.com/reference/android/app/Application.html

As for the Asynctask

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

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

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

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

http://developer.android.com/guide/components/services.html

Browser other questions tagged

You are not signed in. Login or sign up in order to post.