IntentService 和 Service 有什么区别?

发布于 2024-12-10 06:26:32 字数 71 浏览 2 评论 0原文

您能帮我理解 IntentServiceService 之间的区别吗?

Can you please help me understand what the difference between an IntentService and a Service is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

安人多梦 2024-12-17 06:26:33

Service 是服务实现的基类。 Service 在应用程序的主线程上运行,这可能会降低应用程序的性能。因此,IntentService(Service 的直接子类)可以使事情变得更容易。

IntentService 用于在后台执行特定任务。完成后,IntentService 实例会自动终止。其使用示例是从互联网下载特定资源。

差异

  1. Service 类使用应用程序的主线程,而 IntentService 创建一个工作线程并使用该线程来运行服务。
  2. IntentService 创建一个队列,一次将一个意图传递给 onHandleIntent()。因此,实现多线程应该通过直接扩展Service类来实现。
    Service 类需要使用 stopSelf() 手动停止。同时,IntentService在执行完成后会自动停止。
  3. IntentService 实现返回 nullonBind()。这意味着IntentService默认无法绑定。
  4. 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 of IntentService terminates itself automatically. Examples for its usage would be to download a certain resource from the Internet.

Differences

  1. Service class uses the application's main thread, while IntentService creates a worker thread and uses that thread to run the service.
  2. IntentService creates a queue that passes one intent at a time to onHandleIntent(). Thus, implementing a multi-thread should be made by extending Service class directly.
    Service class needs a manual stop using stopSelf(). Meanwhile, IntentService automatically stops itself when it finishes execution.
  3. IntentService implements onBind() that returns null. This means that the IntentService can not be bound by default.
  4. IntentService implements onStartCommand() that sends Intent to queue and to onHandleIntent().

In brief, there are only two things to do to use IntentService. Firstly, to implement the constructor. And secondly, to implement onHandleIntent(). For other callback methods, the super is needed to be called so that it can be tracked properly.

何止钟意 2024-12-17 06:26:33

简而言之,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

偷得浮生 2024-12-17 06:26:33

服务:它在系统后台运行。例如,

  1. 如果您去酒店,将汤订单交给服务员,
  2. 服务员收到您的订单并发送给厨师,
  3. 您不知道厨房里的汤是如何制作的,也不知道制作汤需要哪些过程。汤
  4. 一旦你的订单准备好,服务员就会给你端上汤。

后台进程:厨师做汤

IntentService:- 这是连续服务..(即)当您一次点很多食物到服务器,但服务器将这些项目一件一件地交付而不是全部交付时一次。

service: It runs in the background on your system. For example,

  1. If you went to a hotel and you give your order for a soup to a server
  2. The server gets your order and sends to chef
  3. You don't know how the soup is made in the kitchen and what processes are required for making the soup
  4. Once your order is ready, the server brings you the soup.

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.

甜中书 2024-12-17 06:26:33

请参阅 Tejas Lagvankar 关于此主题的帖子
以下是 Service 和 IntentService 以及其他组件之间的一些关键区别。

在此处输入图像描述

See Tejas Lagvankar's post about this subject.
Below are some key differences between Service and IntentService and other components.

enter image description here

孤城病女 2024-12-17 06:26:33

Service

  • 没有 UI 的任务,但不应该用于长任务。在服务中使用长线程 任务
  • onStartService()
  • 调用 从任何线程
  • 触发 在主线程上运行
  • 可能会阻塞 main(UI) 线程

IntentService

  • Long 来完成
  • 任务通常不与主线程通信,如果需要通信,则由处理程序或广播通过从主线程触发的 Intent 调用
  • 在主线程上接收 Intent 并生成工作线程
  • ( 在单独的线程上
  • 我们无法并行运行任务,并且多个意图在同一工作线程上排队。

Service

  • Task with no UI,but should not use for long Task. Use Thread within service for long Task
  • Invoke by onStartService()
  • Triggered from any Thread
  • Runs On Main Thread
  • May block main(UI) thread

IntentService

  • Long task usually no communication with main thread if communication is needed then it is done by Handler or broadcast
  • Invoke via Intent
  • triggered from Main Thread (Intent is received on main Thread and worker thread is spawned)
  • Runs on separate thread
  • We can't run task in parallel and multiple intents are Queued on the same worker thread.
蓝海 2024-12-17 06:26:33

Service 实际上在应用程序的同一线程中运行;当您扩展 Service 时,您必须手动生成新线程来运行 CPU 阻塞操作。

vs

IntentServiceService 的子类,它生成一个线程来执行后台工作(无需创建新线程来执行 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 of Service which spawns a thread to do background work from there(No need to create a new thread to do CPU blocking operations).

半城柳色半声笛 2024-12-17 06:26:33

Service:在主线程中工作,因此几秒钟后会导致ANR(Android 无响应)。

IntentServiceService 与另一个后台线程单独工作,在不与主线程交互的情况下执行某些操作。

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.

彼岸花似海 2024-12-17 06:26:33

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文