3

I have setup an application to perform syncing with the Android syncing framework. Most of the internals have been taken from the sync adapter demo. I have a very simple question though, when does this app sync? I know that the google services will sync when they receive a network "tickle" is this also the case with services that you have setup to sync?

Brian Griffey
  • 4,751
  • 1
  • 25
  • 26

2 Answers2

5

As Amorgos mentioned those tickles you mentioned are C2DM push messages. If it is important to sync changes instantly after they are made in the cloud you should think about implementing them. You can go here for more information.

For requesting a sync operation, the class you have to look at is the ContentResolver. If you want to sync call requestSync(Account account, String authority, Bundle extras) (use it for a sync now button or similar). To sync changes in your ContentProvider you can call

getContentResolver().notifyChange(CONTENT_URI, null, true);  

after creating/changing the entry (for changes that shouldn't be synced replace the true with false and no sync will be triggered). If android:supportsUploading in your SyncAdapter's xml file is set to true, this will automatically trigger a sync. In this case the Bundle in your SyncAdapter contains a Boolean value with the key ContentResolver.SYNC_EXTRAS_UPLOAD which is true. You can use it to just sync local changes to the cloud and not querying anything.

If you want just to sync each hour use addPeriodicSync(Account account, String authority, Bundle extras, long pollFrequency).

You can also use the ContentResolver to read/set if it should sync or not (the value which is shown in the device settings at accounts & sync). The methods are getIsSyncable(...) and setIsSyncable(...).

I hope this can help you.

Edit: This also describes the processes really good: Why does ContentResolver.requestSync not trigger a sync?

Community
  • 1
  • 1
Gabriel Ittner
  • 1,162
  • 9
  • 22
1

It all depends on the requirements of your application. If you're syncing data which is not critical that the user knows about straight away, then the OS initiating sync might be enough. (A list of contacts is a perfect example of this)

If your app relies on being notified in a more real-time way then you should consider using C2DM push notifications to initiate the sync process. You can raise C2DM messages when the server modifies data and it sends them to your device. The app will then run your sync process based on the contents this message. (C2DM is the network tickle you refer to for Gmail for example)

It is up to you to architect your application to decide when to efficiently initiate that sync using whatever way you decide. All apps have different requirements for sync.

Eurig Jones
  • 8,226
  • 7
  • 52
  • 74