Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
174 views
in Technique[技术] by (71.8m points)

android - 服务与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.

(如果有人可以帮助我解决我的两个问题,我将不胜感激。)

  ask by roiberg translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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() 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...