0

I am a beginner of Android Programming, and when I learn something about Service ,the problem has become. We all know that the UI thread cannot run a long-time process, so we should run them in a new thread, and immediately ,we have recognised that something about Handler AsyncTask and Service even send a BroadCast when the task has been finished.However, I am not sure when to use them.

For example, we often using an activity to login, when the data should be posted, maybe post to the remote server, it may cost a long-time,we cannot write something in LoginActivity maybe in an AsyncTask or a Service to do that.But which is the better choice ?

Konrad Krakowiak
  • 12,285
  • 11
  • 58
  • 45
Kai Zhao
  • 3
  • 2
  • 4
    possible duplicate of [Difference between Service, Async Task & Thread?](http://stackoverflow.com/questions/3264383/difference-between-service-async-task-thread) – Fahim Apr 10 '15 at 07:59

3 Answers3

0

A service is running all the time on background on your device.

An AsynkTack is launch when you need it and executed in background. When it's done, the thread is destroyed.

Maxouille
  • 2,729
  • 2
  • 19
  • 42
  • So a Service is trying to provide something the application always need ,and the AsyncTask is providing some result with less time than a Service ? – Kai Zhao Apr 10 '15 at 08:11
  • Actually a service is common used to have a task running always in background while an asynctask is a punctual task. Check this post, it is very well explained : http://stackoverflow.com/questions/3264383/difference-between-service-async-task-thread – Maxouille Apr 10 '15 at 08:19
0

As a general rule:

  • A service is running in background and does some (periodic) work for you. For example if you want to fetch news from a REST-API every 15 minutes and informed the user within the app or with a notification than use a service.
  • An AsyncTask is an separated thread for work intensive jobs to prevent the UI thread from blocking. For example fetch the news after the user clicks the "Get News" button.

In your case, the expectation of the user is important. The user tries to login and waits in an activity for the responds of the server. You should send the request to the server in an AsyncTask and e.g. show the user a waitscreen until the response is there ("Try to login"). So the UI is still responsive but the user clearly knows, that he has to wait for a response.

dbaelz
  • 144
  • 4
0

Services - A Service is an application component that can perform long-running operations in the background, and it does not provide a user interface.A service is a task with no UI.Runs always on main thread and can also block main thread.We can start service by calling method startService()

AysnTask - The AsyncTask class allows to run instructions in the background and to synchronize again with the main thread. It also reporting progress of the running tasks.Used for task in parallel.We start it by calling method execute().Wrok on worker thread and triggered from main thread.It can not run in loop.

Ankita Singh
  • 304
  • 3
  • 17