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

标签 android android-service android-syncadapter android-background

为什么下面 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/

相关文章:

android - Gradle 中新的 Implement 关键字是什么

java - 如何延迟重复警报的触发(开始时间)?

android - SyncAdapter periodicsync() 不触发

android - 如何在 SyncAdapter onPerformSync 期间正确使用 AsyncTask

android - 在不创建帐户的情况下使用 SyncAdapter

android - 为什么发布构建抛出错误,但在 buildTypes 相同时不进行调试? (Android gradle 构建类型)

android - Unity 3d 中的 PlayerPrefs 不起作用(有时)

android - Bitbucket - 将项目文件推送到空的 Git 存储库

Android Activity 服务通信

Android Application onCreate,什么时候调用