Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Service vs Thread in Android

I am looking for what service should be used in android applicaton.

Docs says

A Service is an application component that can perform long-running operations in the background and does not provide a user interface.

I have read this thread Application threads vs Service threads that saying same services are for running operation in background.

But here this can be done using Thread also. Any difference between them and where you should use them

like image 588
N Sharma Avatar asked Apr 08 '14 10:04

N Sharma


People also ask

What is the difference between service and thread in Android?

Service : is a component of android which performs long running operation in background, mostly with out having UI. Thread : is a O.S level feature that allow you to do some operation in the background.

What is difference between service and thread in Android Mcq?

Explanation. Services, by default, work on Main thread. You can start services from any thread, but if you want to update the UI, you need to call Main thread.

What is difference between service and AsyncTask in Android?

service is like activity long time consuming task but Async task allows us to perform long/background operations and show its result on the UI thread without having to manipulate threads.

What is the difference between activity and services in Android?

Services are a unique component in Android that allows an application to run in the background to execute long-running operation activities, on the other hand, an activity, like a window or a frame in Java, represents a single screen with a user interface.


2 Answers

UPDATE based on latest documentation:

Android has included in its documentation on when you should use Service vs Thread. Here is what it says:

If you need to perform work outside your main thread, but only while the user is interacting with your application, then you should probably instead create a new thread and not a service. For example, if you want to play some music, but only while your activity is running, you might create a thread in onCreate(), start running it in onStart(), then stop it in onStop(). Also consider using AsyncTask or HandlerThread, instead of the traditional Thread class. See the Processes and Threading document for more information about threads.

Remember that if you do use a service, it still runs in your application's main thread by default, so you should still create a new thread within the service if it performs intensive or blocking operations.

Another notable difference between these two approaches is that Thread will sleep if your device sleeps. Whereas, Service can perform operation even if the device goes to sleep. Let's take for example playing music using both approaches.

Thread Approach: the music will only play if your app is active or screen display is on.

Service Approach: the music can still play even if you minimized your app or screen is off.

Note: Starting API Level 23, you should Test your app with Doze.

Android Documentation - Services

like image 101
user1506104 Avatar answered Sep 18 '22 17:09

user1506104


A Service is meant to run your task independently of the Activity, it allows you to run any task in background. This run on the main UI thread so when you want to perform any network or heavy load operation then you have to use the Thread there.

Example : Suppose you want to take backup of your instant messages daily in the background then here you would use the Service.

Threads is for run your task in its own thread instead of main UI thread. You would use when you want to do some heavy network operation like sending bytes to the server continuously, and it is associated with the Android components. When your component destroy who started this then you should have stop it also.

Example : You are using the Thread in the Activity for some purpose, it is good practice to stop it when your activity destroy.

like image 27
N Sharma Avatar answered Sep 20 '22 17:09

N Sharma