3

I have used Google's Android developer documentation (http://developer.android.com/training/sync-adapters) to create a syncing system.

My app now has an entry in Android Settings under Accounts. When I tap this entry, I see the sync adaptor I have configured. Everything looks ok except that the box is unchecked and it says "Sync is OFF".

I have Googled and Stackoverflowed and there seems to be one way to enable sync, as explained here: https://stackoverflow.com/a/5279191/127434. The answer at that link says to call ContentResolver.setSyncAutomatically(account, authority, true).

However, Google's docs (http://developer.android.com/training/sync-adapters/running-sync-adapter.html#RunByNetwork) say:

When a network connection is available, the Android system sends out a message every few seconds to keep the device's TCP/IP connection open. This message also goes to the ContentResolver of each app. By calling setSyncAutomatically(), you can run the sync adapter whenever the ContentResolver receives the message.

But I don't want to sync every few seconds. (I'd like to use ContentResolver.addPeriodicSync to schedule a sync a few times an hour.)

So how can I enable syncing for my adaptor without having it sync every few seconds?

Community
  • 1
  • 1
cja
  • 9,512
  • 21
  • 75
  • 129
  • Are you saying setSyncAutomatically is required for addPeriodicSync? – Eric Woodruff Mar 05 '14 at 19:19
  • I mean my third paragraph exactly as I wrote it – cja Mar 05 '14 at 19:46
  • It isn't clear whether you found the addPeriodicSync to be dependent on the automatic sync which you don't want even though it is recommended by google. – Eric Woodruff Mar 05 '14 at 20:01
  • Where is it recommended by Google? That's my point - I can't see that Google recommend this but equally this seems to be the only way to do it – cja Mar 05 '14 at 21:19
  • 2
    Were you ever able to find a solution to this? I can't get addPeriodicSync to work at all. setSyncAutomatically(_,_,_,true) causes the sync adapter to run every minute, and if I pass false to it or don't call it at all, sync is disabled for the account. addPeriodicSync seems to just be ignored. I've tried adding a label to the ContentProvider, calling setIsSyncable, etc. – kldavis4 Feb 12 '16 at 17:35

1 Answers1

0

You should add ContentResolver.setMasterSyncAutomatically(true); to your code. Without this the setSyncAutomatically will be ignored.

Read the documentation [here] (http://developer.android.com/reference/android/content/ContentResolver.html#setMasterSyncAutomatically(boolean))

Roadblock
  • 2,041
  • 2
  • 24
  • 38