gpt4 book ai didi

android - 为什么要使用 Service 而不是 IntentService?

转载 作者:行者123 更新时间:2023-11-29 17:45:42 26 4
gpt4 key购买 nike

为什么下面 Google 提供的 SyncService 示例使用 Service 而不是 IntentService?据我了解,IntentServices 在后台运行,而常规 Service 将在主线程中运行。对于没有 UI 只是更新数据的东西,你为什么要让它在主线程中运行?这不会有丢帧的风险吗?

http://developer.android.com/training/sync-adapters/creating-sync-adapter.html#CreateSyncAdapterService

是否可以有一个独立的IntentService?或者它是否需要基于主线程上运行的东西?这是我能理解为什么我们会使用上面的常规 Service 的唯一原因。

最佳答案

It is my understanding that IntentServices run in the background, while a regular Service will run in the main thread.

对象不在线程上运行。方法可以。

IntentService 继承自 ServiceService 上的主要生命周期方法,特别是 onStartCommand() 在主应用程序线程上调用。 IntentService 恰好提供了一个后台线程,它用于调用您的 onHandleIntent() 方法,该方法由对 onStartCommand() 的调用触发。

For something with no UI that just updates data, why would you want it run in the main thread?

你不知道。

Why does the SyncService example provided by Google below use a Service instead of an IntentService?

因为 IntentService 在这里不合适。引用您链接到的文档:

To do this, you need to create a bound Service that passes a special Android binder object from the sync adapter component to the framework. With this binder object, the framework can invoke the onPerformSync() method and pass data to it.

IntentService 和绑定(bind)模式不能很好地协同工作。

从线程的角度来看,onPerformSync() is called on a background thread , 由 Android 提供。因此,即使未根据绑定(bind)排除 IntentService,您也不需要 另一个 后台线程,因为 onPerformSync() 已被调用后台线程。

关于android - 为什么要使用 Service 而不是 IntentService?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26975874/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com