Android 中的服务与线程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22933762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Service vs Thread in Android
提问by N Sharma
I am looking for what service should be used in android applicaton.
我正在寻找应该在 android 应用程序中使用什么服务。
Docs says
文档说
A Service is an application component that can perform long-running operations in the background and does not provide a user interface.
Service 是一个应用程序组件,可以在后台执行长时间运行的操作,并且不提供用户界面。
I have read this thread Application threads vs Service threadsthat 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
但在这里也可以使用Thread
。它们之间的任何区别以及您应该在哪里使用它们
采纳答案by N Sharma
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.
服务旨在独立于 运行您的任务Activity
,它允许您在后台运行任何任务。这在主 UI 线程上运行,因此当您想要执行任何网络或重负载操作时,您必须使用Thread
那里。
Example : Suppose you want to take backup of your instant messages daily in the background then here you would use the Service
.
示例:假设您想每天在后台备份即时消息,那么在这里您将使用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.
Threads
用于在自己的线程而不是主 UI 线程中运行您的任务。当您想要执行一些繁重的网络操作(例如连续向服务器发送字节)时,您会使用它,并且它与 Android 组件相关联。当您的组件破坏了谁启动它时,您也应该停止它。
Example : You are using the Thread
in the Activity for some purpose, it is good practice to stop it when your activity destroy.
示例:您Thread
出于某种目的在 Activity中使用,当您的 Activity 销毁时停止它是一种很好的做法。
回答by user1506104
UPDATE based on latest documentation:
基于最新文档的更新:
Android has included in its documentation on when you should use Service vs Thread. Here is what it says:
Android 已在其文档中包含有关何时应使用 Service 与 Thread 的信息。这是它所说的:
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.
如果您需要在主线程之外执行工作,但仅在用户与您的应用程序交互时执行,那么您可能应该创建一个新线程而不是服务。例如,如果您想播放一些音乐,但仅在您的活动正在运行时,您可以在 onCreate() 中创建一个线程,在 onStart() 中开始运行它,然后在 onStop() 中停止它。还可以考虑使用 AsyncTask 或 HandlerThread,而不是传统的 Thread 类。有关线程的更多信息,请参阅进程和线程文档。
请记住,如果您确实使用了服务,默认情况下它仍然在应用程序的主线程中运行,因此如果它执行密集或阻塞操作,您仍然应该在服务中创建一个新线程。
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 将休眠。而即使设备进入睡眠状态,Service 也可以执行操作。让我们以使用这两种方法播放音乐为例。
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.
注意:从 API 级别 23 开始,您应该使用 Doze 测试您的应用程序。
回答by Vinay W
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).
- 操作发生时,应用程序必须是可见的。
- 后台运行时间比较短(不到一两分钟)
- 活动/屏幕/应用程序与后台操作高度耦合,用户通常会“等待”此操作完成,然后再在应用程序中执行任何其他操作。在这些情况下使用线程会导致代码更清晰、更具可读性和可维护性。话虽如此,它可以使用服务(或 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.
- 操作发生时应用程序可能不可见(前台服务等功能可以帮助操作中断)
- 用户无需“等待”操作完成即可在应用程序中执行其他操作。
- 应用程序是可见的,并且操作独立于应用程序/屏幕上下文。
回答by Elsa Lin
Reference from https://developer.android.com/guide/components/services.html
来自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().
例如,如果您想播放一些音乐,但仅在您的活动正在运行时,您可以在 onCreate() 中创建一个线程,在 onStart() 中开始运行它,然后在 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.
请记住,如果您确实使用了服务,默认情况下它仍然在应用程序的主线程中运行,因此如果它执行密集或阻塞操作,您仍然应该在服务中创建一个新线程。