服务与IntentService

本文翻译自:Service vs 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进行但不能通过Service (反之亦然)吗?

I also believe that an IntentService runs in a different thread and a Service does not. 我也相信IntentService在不同的线程中运行,而Service在不同的线程中运行。 So, as far as I can see, starting a service within its own thread is like starting an IntentService . 因此,据我所知,在自己的线程中启动服务就像启动IntentService Is it not? 不是吗

I would appreciate if someone can help me with both of my questions. 如果有人可以帮助我解决我的两个问题,我将不胜感激。


#1楼

参考:https://stackoom.com/question/138Zc/服务与IntentService


#2楼

I'm sure you can find an extensive list of differences by simply googling something such as 'Android IntentService vs Service' 我敢肯定,您可以通过简单地搜索“ Android IntentService vs Service”之类的内容找到大量差异

One of the more important differences per example is that IntentService ends itself once it's done. 每个示例最重要的区别之一是IntentService完成后便会自行结束。

Some examples (quickly made up) could be; 一些例子(迅速组成)可能是:

IntentService: If you want to download a bunch of images at the start of opening your app. IntentService:如果要在打开应用程序开始时下载一堆图像。 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. 服务:一种服务,将经常用于通过Web API调用在您的应用程序和后端之间进行通信。 Even if it is finished with its current task, you still want it to be around a few minutes later, for more communication. 即使完成当前任务,您仍然希望它在几分钟后才能获得更多的交流。


#3楼

If someone can show me an example of something that can be done with an IntentService and can not be done with a Service and the other way around. 如果有人可以向我展示一个示例,该示例可以通过IntentService完成,而不能通过Service完成, IntentService

By definition, that is impossible. 根据定义,这是不可能的。 IntentService is a subclass of Service , written in Java. IntentService是用Java编写的Service的子类。 Hence, anything an IntentService does, a Service could do, by including the relevant bits of code that IntentService uses. 因此,任何一个IntentService确实,一个Service可以做,通过包括代码的相关位IntentService用途。

Starting a service with its own thread is like starting an IntentService. 用自己的线程启动服务就像启动IntentService。 Is it not? 不是吗

The three primary features of an IntentService are: IntentService的三个主要功能是:

  • the background thread 后台线程

  • the automatic queuing of Intent s delivered to onStartCommand() , so if one Intent is being processed by onHandleIntent() on the background thread, other commands queue up waiting their turn 传递给onStartCommand()Intent的自动排队,因此,如果一个Intent正在由onHandleIntent()在后台线程上处理,则其他命令排队等候

  • the automatic shutdown of the IntentService , via a call to stopSelf() , once the queue is empty 一旦队列为空,则通过调用stopSelf()自动关闭IntentService

Any and all of that could be implemented by a Service without extending IntentService . 无需扩展IntentService即可通过Service来实现所有这些IntentService


#4楼

An IntentService is an extension of a Service that is made to ease the execution of a task that needs to be executed in background and in a seperated thread. IntentService是Service的扩展,旨在简化需要在后台和单独线程中执行的任务的执行。

IntentService starts, create a thread and runs its task in the thread. IntentService启动,创建线程并在线程中运行其任务。 once done, it cleans everything. 一旦完成,它将清理所有内容。 Only one instance of a IntentService can run at the same time, several calls are enqueued. IntentService的一个实例只能同时运行,将多个调用排队。

It is very simple to use and very convenient for a lot of uses, for instance downloading stuff. 它非常易于使用,并且对于许多用途(例如下载内容)非常方便。 But it has limitations that can make you want to use instead the more basic (not simple) Service. 但是它有局限性,可能使您想使用更基本(而不是简单)的服务。

For example, a service connected to a xmpp server and bound by activities cannot be simply done using an IntentService. 例如,连接到xmpp服务器并受活动限制的服务不能简单地使用IntentService来完成。 You'll end up ignoring or overriding IntentService stuffs. 您最终将忽略或覆盖IntentService。


#5楼

Tejas Lagvankar wrote a nice post about this subject. Tejas Lagvankar就此主题写了一篇不错的文章 Below are some key differences between Service and IntentService. 以下是Service和IntentService之间的一些主要区别。

When to use? 什么时候使用?

  • The Service can be used in tasks with no UI, but shouldn't be too long. Service可以用于没有UI的任务中,但不应太长。 If you need to perform long tasks, you must use threads within Service. 如果需要执行长任务,则必须使用Service中的线程。

  • The IntentService can be used in long tasks usually with no communication to Main Thread. IntentService可以用于长时间任务,通常无需与主线程通信。 If communication is required, can use Main Thread handler or broadcast intents. 如果需要通信,则可以使用主线程处理程序或广播意图。 Another case of use is when callbacks are needed (Intent triggered tasks). 另一个使用情况是需要回调(意图触发的任务)时。

How to trigger? 如何触发?

  • The Service is triggered by calling method startService() . 通过调用方法startService()触发服务

  • The IntentService is triggered using an Intent, it spawns a new worker thread and the method onHandleIntent() is called on this thread. IntentService是使用Intent触发的,它产生一个新的工作线程,并在该线程上调用onHandleIntent()方法。

Triggered From 触发自

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

Runs On 运行

  • The Service runs in background but it runs on the Main Thread of the application. 服务在后台运行,但在应用程序的主线程上运行。

  • The IntentService runs on a separate worker thread. IntentService在单独的工作线程上运行。

Limitations / Drawbacks 局限性/缺点

  • The Service may block the Main Thread of the application. 服务可能会阻止应用程序的主线程。

  • The IntentService cannot run tasks in parallel. IntentService无法并行运行任务。 Hence all the consecutive intents will go into the message queue for the worker thread and will execute sequentially. 因此,所有连续的意图都将进入工作线程的消息队列,并将按顺序执行。

When to stop? 什么时候停止?

  • If you implement a Service , it is your responsibility to stop the service when its work is done, by calling stopSelf() or stopService() . 如果实现服务 ,则有责任通过调用stopSelf()stopService()在服务完成时停止该服务。 (If you only want to provide binding, you don't need to implement this method). (如果只想提供绑定,则不需要实现此方法)。

  • The IntentService stops the service after all start requests have been handled, so you never have to call stopSelf() . 在处理所有启动请求后, IntentService将停止服务,因此您不必调用stopSelf()


#6楼

Adding points to the accepted answer: 在接受的答案中加分:

See the usage of IntentService within Android API. 查看Android API中IntentService的用法。 eg: 例如:

public class SimpleWakefulService extends IntentService {
    public SimpleWakefulService() {
        super("SimpleWakefulService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {  ...}

To create an IntentService component for your app, define a class that extends IntentService, and within it, define a method that overrides onHandleIntent(). 要为您的应用创建IntentService组件,请定义一个扩展IntentService的类,并在其中定义一个覆盖onHandleIntent()的方法。

Also, see the source code of the IntentService, it's constructor and life cycle methods like onStartCommand... 另外,请参阅IntentService的源代码,它是构造函数和生命周期方法,例如onStartCommand ...

  @Override
    public int More ...onStartCommand(Intent intent, int flags, int startId) {
       onStart(intent, startId);
        return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY;
    }

Service together an AsyncTask is one of best approaches for many use cases where the payload is not huge. 对于有效负载不大的许多用例,将AsyncTask一起提供服务是最好的方法之一。 or just create a class extending IntentSerivce. 或者只是创建一个扩展IntentSerivce的类。 From Android version 4.0 all network operations should be in background process otherwise the application compile/build fails. 从Android 4.0版开始,所有网络操作都应处于后台进程,否则应用程序编译/构建将失败。 separate thread from the UI. 与用户界面分开的线程。 The AsyncTask class provides one of the simplest ways to fire off a new task from the UI thread. AsyncTask类提供了从UI线程触发新任务的最简单方法之一。 For more discussion of this topic, see the blog post 有关此主题的更多讨论,请参阅博客文章。

from Android developers guide : 来自Android开发人员指南

IntentService is a base class for Services that handle asynchronous requests (expressed as Intents) on demand. IntentService是Services的基类,可按需处理异步请求(表示为Intent)。 Clients send requests through startService(Intent) calls; 客户端通过startService(Intent)调用发送请求; the service is started as needed, handles each Intent, in turn, using a worker thread, and stops itself when it runs out of work. 服务会根据需要启动,依次使用工作线程处理每个Intent,并在工作用尽时自行停止。

Design pattern used in IntentService IntentService中使用的设计模式

: This "work queue processor" pattern is commonly used to offload tasks from an application's main thread. :此“工作队列处理器”模式通常用于从应用程序的主线程卸载任务。 The IntentService class exists to simplify this pattern and take care of the mechanics. 存在IntentService类是为了简化此模式并注意机制。 To use it, extend IntentService and implement onHandleIntent(Intent). 要使用它,请扩展IntentService并实现onHandleIntent(Intent)。 IntentService will receive the Intents, launch a worker thread, and stop the service as appropriate. IntentService将接收Intent,启动工作线程,并在适当时停止服务。

All requests are handled on a single worker thread -- they may take as long as necessary (and will not block the application's main loop), but only one request will be processed at a time. 所有请求都在单个工作线程上处理-它们可能花费必要的时间(并且不会阻塞应用程序的主循环),但是一次仅处理一个请求。

The IntentService class provides a straightforward structure for running an operation on a single background thread. IntentService类提供了用于在单个后台线程上运行操作的简单结构。 This allows it to handle long-running operations without affecting your user interface's responsiveness. 这使其可以处理长时间运行的操作,而不会影响用户界面的响应能力。 Also, an IntentService isn't affected by most user interface lifecycle events, so it continues to run in circumstances that would shut down an AsyncTask. 另外,IntentService不受大多数​​用户界面生命周期事件的影响,因此它会在可能会关闭AsyncTask的情况下继续运行。

An IntentService has a few limitations: IntentService有一些限制:

It can't interact directly with your user interface. 它不能直接与您的用户界面进行交互。 To put its results in the UI, you have to send them to an Activity. 要将其结果放入用户界面,您必须将其发送到活动。 Work requests run sequentially. 工作请求按顺序运行。 If an operation is running in an IntentService, and you send it another request, the request waits until the first operation is finished. 如果某个操作正在IntentService中运行,并且您向它发送了另一个请求,则该请求将等待直到第一个操作完成。 An operation running on an IntentService can't be interrupted. 在IntentService上运行的操作不能被中断。 However, in most cases 但是,在大多数情况下

IntentService is the preferred way to simple background operations IntentService是进行简单后台操作的首选方式

** **

Volley Library 凌空图书馆

There is the library called volley-library for developing android networking applications The source code is available for the public in GitHub. 有一个名为volley-library的库,用于开发android网络应用程序 。源代码在GitHub上可供公众使用。

The android official documentation for Best practices for Background jobs : helps better understand on intent service, thread, handler, service. 后台作业最佳做法的android官方文档:帮助更好地了解意图服务,线程,处理程序,服务。 and also Performing Network Operations 以及执行网络操作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值