Service vs Thread in Android

2019-01-22 02:41发布

问题:

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

回答1:

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



回答2:

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.



回答3:

This is the principle i largely follow

Use a Thread when

  • app is required to be visible when the operation occurs.
  • background operation is relatively short running (less than a minute or two)
  • the activity/screen/app is highly coupled with the background operation, the user usually 'waits' for this operation to finish before doing anything else in the app. Using a thread in these cases leads to cleaner, more readable & maintainable code. That being said its possible to use a Service( or IntentService).

Use a Service when

  • app could be invisible when the operation occurs (Features like Foreground service could help with operations being interrupted)
  • User is not required to 'wait' for the operation to finish to do other things in the app.
  • app is visible and the operation is independent of the app/screen context.


回答4:

Reference from https://developer.android.com/guide/components/services.html

A service is simply a component that can run in the background even when the user is not interacting with your application. Thus, you should create a service only if that is what you need.

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().

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.



回答5:

Probably you already read the documentation description about them, I won't repeat them, instead I will try to give answer with my own words, hope they will help you.

Service is like an Activity but has no interface. Probably if you want to fetch the weather for example you won't create a blank activity for it, for this you will use a Service.

A Thread is a Thread, probably you already know it from other part. You need to know that you cannot update UI from a Thread. You need to use a Handler for this, but read further.