3

I'm developing an android application that Sync selected accounts at user defined frequencies.

AccountManager manager = (AccountManager) getSystemService(ACCOUNT_SERVICE);
Account[] list = manager.getAccounts();
Pattern emailPattern = Patterns.EMAIL_ADDRESS;
for(i=0;i<AcntCount;i++)
    for (Account account : list) 
        if (emailPattern.matcher(account.name).matches())  
            if(account.name.matches(syncSettings.getString("Account"+i.toString(), null)))
                //ContentResolver.addPeriodicSync(account, authority, extras, pollFrequency)

What are the values that should be given in place of authority and extras in the addPeriodicSync(). Do I have to write a sync-adapter for this?

Rob
  • 4,927
  • 12
  • 49
  • 54
JiTHiN
  • 6,548
  • 5
  • 43
  • 69
  • 1
    Authority would be the authority of the content provider you want to synchronize, for contacts this would be "`com.android.contacts`". The extras `Bundle` is used to pass any parameters you need to specify for that particular sync adapter / sync operation - refer to the constants named `SYNC_EXTRA_xxx` in `ContentResolver` for hints about these. – Jens Jul 24 '12 at 08:44
  • Thanks Jens. To sync both calendar and contacts should i write separate `addPeriodicSync()`? – JiTHiN Jul 24 '12 at 09:23
  • Yup, there's no "catch-all" authority you can pass as far as I know. You should however check if the sync adapter is configured to sync the authority beforehand by calling `ContentResolve#getIsSyncable(..)` – Jens Jul 25 '12 at 06:43

1 Answers1

0

Yes. You have to write a SyncAdapter. addPeriodicSync() is specifically used to tell ContentResolver to cause a SyncAdapter to fire on a timer.

What authority do you need? Well, SyncAdapters work on a (Account,Authority) pair.

The account specifies the specific login -- you might have multiple if you have more than one gmail address, for example.

The Authority tells the kind of data -- For example, with a gmail acocunt, you might be synchronizing... mail, contacts, calendar, finance, etc... So this tells you the type of data and the service that's bound to the account.

The pair together is provided to SyncAdapter -- More specifically you have a bunch of syncAdapters on the system (They define the authorities that are available to sync), and the correct syncadapter is looked up when PeriodicSync fires.

So if you're writing your own app that synchronizes against your own custom server, you'll write a SyncAdapter that knows how to read and store your schema, and you'll define an authority. These are just a string, and it's usually in java-classpath style. -- "com.example.SyncAdapterSample"

Read Why does ContentResolver.requestSync not trigger a sync? for all the steps you need to get a working SyncAdapter. It's definitely the way you want to go. A lot of steps to get it set up, but once the framework is there, it makes your life so much easier.

Now -- Extras. These are just some options for how the periodic sync will be done. You should be fine with the defaults, at first. Then dig into The documents. For example, the extra SYNC_EXTRAS_EXPEDITED indicates that this is a high priority update that should be synced right away -- usually, the scheduler waits a minute or two so that it can batch requests (and save battery, by not having the radio on all the time, but only in concentrated bursts). In general, you shouldn't need to worry about extras until your SyncAdapter gets much more complex than a simple baseline one.

Community
  • 1
  • 1
jcwenger
  • 11,383
  • 1
  • 53
  • 65