服务VS IntentService [英] Service vs IntentService

查看:127
本文介绍了服务VS IntentService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人可以告诉我的东西,可以用 IntentService ,不能用服务做(反之亦然)?

Can someone please show me an example of something that can be done with an IntentService that cannot be done with a Service (and vice-versa)?

我也相信,一个 IntentService 在不同的线程运行和服务没有。所以,就我所看到的,开始在自己的线程中的服务就像启动一个 IntentService 。是不是?

I also believe that an IntentService runs in a different thread and a Service does not. So, as far as I can see, starting a service within its own thread is like starting an IntentService. Is it not?

我会AP preciate如果有人能帮助我与我的两个问题。

I would appreciate if someone can help me with both of my questions.

推荐答案

Tejas的Lagvankar写了一个漂亮的帖子关于这个主题。 下面是服务和IntentService之间的一些关键区别。

Tejas Lagvankar wrote a nice post about this subject. Below are some key differences between Service and IntentService.

何时使用?

  • 服务的可以在没有用户界面任务中使用,但不宜过长。如果您需要执行长期任务,必须服务中使用线程。

  • The Service can be used in tasks with no UI, but shouldn't be too long. If you need to perform long tasks, you must use threads within Service.

IntentService 的可用于长期的任务通常与主线程没有通讯。如果沟通是必需的,可以使用主线程处理或广播意图。用另一种情况是当需要回调(意向触发任务)。

The IntentService can be used in long tasks usually with no communication to Main Thread. If communication is required, can use Main Thread handler or broadcast intents. Another case of use is when callbacks are needed (Intent triggered tasks).

如何触发?

  • 服务的是通过调用方法所触发 startService()

  • The Service is triggered by calling method startService().

IntentService 的使用意图,它产生一个新的工作线程和方法 onHandleIntent()被触发被称为这个主题。

The IntentService is triggered using an Intent, it spawns a new worker thread and the method onHandleIntent() is called on this thread.

引发了

  • 服务 IntentService 的可以从任何线索,活动或其他应用程序组件触发。
  • The Service and IntentService may be triggered from any thread, activity or other application component.

运行

  • 服务的在后台运行,但它运行的应用程序的主线程。

  • The Service runs in background but it runs on the Main Thread of the application.

IntentService 的运行在一个单独的工作线程。

The IntentService runs on a separate worker thread.

限制/缺点

  • 服务的可能会阻止应用程序的主线程。

  • The Service may block the Main Thread of the application.

IntentService 的不能并行运行的任务。因此,所有连续的意图将进入消息队列的工作线程,并会按顺序执行。

The IntentService cannot run tasks in parallel. Hence all the consecutive intents will go into the message queue for the worker thread and will execute sequentially.

何时停止?

  • 如果您实施的服务的,它是你的责任,停止该服务时,它的工作完成后,通过调用 stopSelf() stopService()。 (如果你只希望提供绑定,你不需要实现这个方法)。

  • If you implement a Service, it is your responsibility to stop the service when its work is done, by calling stopSelf() or stopService(). (If you only want to provide binding, you don't need to implement this method).

IntentService 的停止后都开始请求都被处理的服务,让您再也不用叫 stopSelf()

The IntentService stops the service after all start requests have been handled, so you never have to call stopSelf().

这篇关于服务VS IntentService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆