Android interview: Services, AsyncTask and Thread

mauriciomunoz profile image Miguel Munoz ・3 min read

The next topic in my head is about other important question in the theory for Android: Services, AsyncTask and Thread.

 What is a Service?

In my own understanding and after I read a lot of documentation, a service is a task used to run for a long time in the background.

An example of this, if you want to play music, download a big file, constantly checking an event, etc.

There are a different kind of services:

Foreground service: this kind of service notify to the user that the service is running. Usually with a notification. When you download an image in any browser you can see a notification for it.

Background service: this service no shows anything. Is used to perform tasks as compress data or upload some status or information to a server.

Bound service: A service doesn't have a UI. So, if you need to inform something to the user, you will need to Bind the service to an Activity. When you play a song, you can get the length of the song or maybe the time where the music is going. To get this event a Bound Service is used.

When we talk about Services, we can't forget the IntentServices. An IntentService is a kind of service used to run a queue of tasks. This help us to do several tasks in the background one by one. One example of this could be when you download a playlist in Spotify or download a bunch of files from WhatsApp. An IntentService enqueue each task and when it finishes the Services is finished to start the next one.

So, how about AsyncTask?

AsyncTask is another way to do work in the background, but, differently to the Service, AsyncTask is used to perform short-running tasks.

If you need to load an image, or maybe to get some information from a server, or any process you know won't take much time, AsyncTask is there for the work.

Is necessary to remember that AsyncTask usually is attached to an Activity, this could be just to load something into the UI or maybe to get information to be used in another process. In any case, if the activity is destroyed before the AsyncTask finishes his process, could cause a memory leak, because the AsyncTask will have the old reference to the activity and if needs to update UI that has been destroyed could cause a crash.

And finally, what are those things called Threads?

A thread is another way to perform a background task. In this case, a Thread runs in his own thread until the task is finished. This is useful when you try to download files or load information and you want to avoid ARNs.

A thread will be finished by itself, so, if we need to perform long-running task we are going to need a Loop, which is an element to keep running a thread until we need to finish.

In the same way, if we need to get information from the thread, we are going to need a Handler. This component will help us to communicate with the thread (remember, a thread runs in his own thread).

I hope this information can help to understand in a better way how this elements works and why are similar. In some interviews this question is important before to go to the technical interview, it shows you have the main understanding of how Android works.

If any of my post have mistakes, I am open to any feedback. Sometimes I could do bad explanation, so any comment is welcome. Together we can create a better guide to study and learn.


Editor guide