android 聊天应用程序的同步方法

Syncing mehod for android chat application

我正在 android 中实现一个聊天应用程序。这个应用程序的一个重要部分是与服务器和本地数据库同步。有几种方法可以在服务器和 android 设备之间同步数据,例如 AsyncTask、IntentService 和 SyncAdapter

我更喜欢使用 SyncAdapter,因为它更高效并且它自己处理大部分后台任务。

当我阅读 SyncAdapter 的开发者页面时,我发现了这个,

Note: Sync adapters run asynchronously, so you should use them with the expectation that they transfer data regularly and efficiently, but not instantaneously. If you need to do real-time data transfer, you should do it in an AsyncTask or an IntentService.

这是否意味着聊天应用程序不好用?

我还需要提一下 SyncAdapter 的一个特性

Automated execution Allows you to automate data transfer based on a variety of criteria, including data changes, elapsed time, or time of day. In addition, the system adds transfers that are unable to run to a queue, and runs them when possible.

所以如果它在数据变化时开始同步(因为新消息存储在sqlite数据库中),我认为SyncAdapter将是Chat App的一个不错的选择。

如有任何建议,我们将不胜感激。

谢谢。

通常移动应用程序取决于后端实现和应用程序要求,但通常您不应该将此类方法用于聊天应用程序,它们不会为您提供最新数据。

我会说,当应用程序在后台时,您应该使用 GCM 进行新消息通知,而当应用程序在前台时,使用 RPC、xmpp、套接字或任何保持连接有效的东西。