IntentService 和 Service 有什么区别?
您能帮我理解 IntentService
和 Service
之间的区别吗?
Can you please help me understand what the difference between an IntentService
and a Service
is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
Service
是服务实现的基类。Service
在应用程序的主线程上运行,这可能会降低应用程序的性能。因此,IntentService(Service 的直接子类)可以使事情变得更容易。IntentService
用于在后台执行特定任务。完成后,IntentService
实例会自动终止。其使用示例是从互联网下载特定资源。差异
Service
类使用应用程序的主线程,而IntentService
创建一个工作线程并使用该线程来运行服务。IntentService
创建一个队列,一次将一个意图传递给onHandleIntent()
。因此,实现多线程应该通过直接扩展Service
类来实现。Service
类需要使用stopSelf()
手动停止。同时,IntentService
在执行完成后会自动停止。IntentService
实现返回null
的onBind()
。这意味着IntentService
默认无法绑定。IntentService
实现onStartCommand()
,将 Intent 发送到队列和onHandleIntent()
。简而言之,使用 IntentService 只需要做两件事。首先,实现构造函数。其次,实现
onHandleIntent()
。对于其他回调方法,需要调用super才能正确跟踪。Service
is a base class of service implementation.Service
runs on the application's main thread which may reduce the application performance. Thus,IntentService
, which is a direct subclass of Service is available to make things easier.The
IntentService
is used to perform a certain task in the background. Once done, the instance ofIntentService
terminates itself automatically. Examples for its usage would be to download a certain resource from the Internet.Differences
Service
class uses the application's main thread, whileIntentService
creates a worker thread and uses that thread to run the service.IntentService
creates a queue that passes one intent at a time toonHandleIntent()
. Thus, implementing a multi-thread should be made by extendingService
class directly.Service
class needs a manual stop usingstopSelf()
. Meanwhile,IntentService
automatically stops itself when it finishes execution.IntentService
implementsonBind()
that returnsnull
. This means that theIntentService
can not be bound by default.IntentService
implementsonStartCommand()
that sends Intent to queue and toonHandleIntent()
.In brief, there are only two things to do to use
IntentService
. Firstly, to implement the constructor. And secondly, to implementonHandleIntent()
. For other callback methods, the super is needed to be called so that it can be tracked properly.简而言之,Service 是开发人员设置后台操作的更广泛的实现,而 IntentService 对于“即发即忘”操作很有用,负责后台线程的创建和清理。
来自文档:
服务
服务是一个应用程序组件,代表应用程序希望在不与用户交互的情况下执行长时间运行的操作,或者提供功能供其他应用程序使用。
意图服务
Service 是 IntentService 的基类按需处理异步请求(表示为 Intents)的服务。客户端通过
startService(Intent)
调用发送请求;该服务根据需要启动,使用工作线程依次处理每个 Intent,并在工作完成时自行停止。请参阅此文档 - http://developer.android.com/reference/android/app /IntentService.html
In short, a Service is a broader implementation for the developer to set up background operations, while an IntentService is useful for "fire and forget" operations, taking care of background Thread creation and cleanup.
From the docs:
Service
A Service is an application component representing either an application's desire to perform a longer-running operation while not interacting with the user or to supply functionality for other applications to use.
IntentService
Service is a base class for IntentService Services that handle asynchronous requests (expressed as Intents) on demand. Clients send requests through
startService(Intent)
calls; the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.Refer this doc - http://developer.android.com/reference/android/app/IntentService.html
服务:它在系统后台运行。例如,
后台进程:厨师做汤
IntentService:- 这是连续服务..(即)当您一次点很多食物到服务器,但服务器将这些项目一件一件地交付而不是全部交付时一次。
service: It runs in the background on your system. For example,
background process: chef making soup
IntentService:- it's consecutive service.. (i.e) when you order many food items at a time to server but the server delivers those items one by one and not deliver them all at once.
请参阅 Tejas Lagvankar 关于此主题的帖子。
以下是 Service 和 IntentService 以及其他组件之间的一些关键区别。
See Tejas Lagvankar's post about this subject.
Below are some key differences between Service and IntentService and other components.
Service
onStartService()
main(UI
) 线程IntentService
Service
onStartService()
main(UI
) threadIntentService
Service
实际上在应用程序的同一线程中运行;当您扩展 Service 时,您必须手动生成新线程来运行 CPU 阻塞操作。vs
IntentService
是Service
的子类,它生成一个线程来执行后台工作(无需创建新线程来执行 CPU 阻塞操作)。Service
runs actually in the same thread of your app; when you extends Service, you must manually spawn new threads to run CPU blocking operations.vs
IntentService
is a subclass ofService
which spawns a thread to do background work from there(No need to create a new thread to do CPU blocking operations).Service
:在主线程中工作,因此几秒钟后会导致ANR(Android 无响应)。IntentService
:Service
与另一个后台线程单独工作,在不与主线程交互的情况下执行某些操作。Service
: Works in the main thread so it will cause an ANR (Android Not Responding) after a few seconds.IntentService
:Service
with another background thread working separately to do something without interacting with the main thread.Intent 服务是 Service
IntentService 的子项:如果您想在打开应用程序时下载一堆图像。这是一个一次性过程,一旦下载完所有内容就可以自行清理。
服务:将不断用于通过 Web API 调用在您的应用程序和后端之间进行通信的服务。即使它完成了当前的任务,您仍然希望它在几分钟后出现,以便进行更多的沟通
Intent service is child of Service
IntentService: If you want to download a bunch of images at the start of opening your app. It's a one-time process and can clean itself up once everything is downloaded.
Service: A Service which will constantly be used to communicate between your app and back-end with web API calls. Even if it is finished with its current task, you still want it to be around a few minutes later, for more communication