当SyncAdapter运行于Android的同步? [英] When SyncAdapter runs synchronization on android?

查看:269
本文介绍了当SyncAdapter运行于Android的同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说,我的应用程序实现SyncAdapter功能,并没有定义周期同步。当同步将在这样的情况下发生的呢?第一个场景中我可能想到的是本地ContentProvided /数据库内容的变化。 什么是有关服务器的变化? SyncAdapter将如何知道的?

Let's say, my application implements SyncAdapter functionality and doesn't define periodic syncs. When synchronization will happen in such scenario? First scenario I may think about is local ContentProvided/database content change. What is about server changes? How SyncAdapter will know about that?

推荐答案

如果你没有定期同步设置,同步会发生,如果你的code明确要求 ContentResolver.requestSync(客户账户,串权威,捆绑演员)您的帐户和权限。

If you have no periodic sync setup, Sync will happen if your code explicitly calls ContentResolver.requestSync(Account account, String authority, Bundle extras) with your account and authority.

另外,如果您的ContentProvider插入,更新或删除函数调用 ContentResolver.notifyChange(URI URI,ContentObserver观测,布尔syncToNetwork),如果布尔syncToNetwork为true(默认值),它也将触发同步。有一个短暂的延迟在这里引起,确保一批数据库的变化不仅会导致一个同步,每改变一个也没有。请注意,您的code应该叫有NotifyChange ,因为它的信号是如何的Andr​​oid你的UI,该UI是反映已经改变的内容之后更新。

Also, if your ContentProvider insert or update or delete functions call ContentResolver.notifyChange(Uri uri, ContentObserver observer, boolean syncToNetwork), if the bool syncToNetwork is true (the default), it will also trigger a sync. There's a short delay induced here, to ensure that a batch of database changes only causes one sync, not one per change. Note that your code should be calling notifyChange because it's how Android signals your UI to update after Content that the UI is reflecting had been changed.

如果服务器数据库的变化,您的应用程序不会知道,因为同步没有发生。有两个选项:

If the server database changes, your app won't know, because sync isn't happening. Two options:

  1. 使用周期同步。这将是清洁的,如果你的服务器API实现的ETag或者如果 - 修改 - 因为HTTP头进行过滤同步因此数据只更新下来。
  2. C2DM (云2设备信息)从本质上讲,推送通知为Android。需要某些服务器组件 - 你打一个设备ID的帐户在服务器上,当服务器的变化,它有明确消息发送到设备,告诉它更新。这是自code工作在服务器上的具体支持Android,但一旦你投资的时候,它的伟大。 C2DM的是Android如何获得Gmail的展现在您的设备就可以到达你的收件箱10秒后,而不是在下次10分钟定期同步。它也更高效的电池,因为你只打开收音机和同步,当你的知道的有新的数据来获得。
  1. Use periodic sync. This will be cleaner if your server API implements etags or the if-modified-since http headers to filter the data you sync so only the updates come down.
  2. C2DM (Cloud 2 Device Messaging) Essentially, push notification for Android. Requires some server components -- You tie a device ID to an account on the server and when the server changes, it has to explicitly send a message to the device to tell it to update. This is custom code work on the server to support android specifically, but once you invest the time, it's great. C2DM is how Android gets gmail to show up on your device 10 seconds after it arrives in your inbox, rather than at the next 10 minute periodic sync. It's also more battery efficient since you only turn on the radio and sync when you know there's new data to get.

这篇关于当SyncAdapter运行于Android的同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆