How do I use the Android SyncAdapter?

xpepermint picture xpepermint · Oct 22, 2010 · Viewed 47k times · Source

I try to understand the Android synchronization logic. What I don't understand is the file syncadapter.xml contained in the Android SDK sample project SampleSyncAdapter. If you downloaded the SDK samples it should be in the following folder:

SDK/android-sdk-PLATFORM/samples/android-VERSION/SampleSyncAdapter/res/xml/syncadapter.xml

I read, the authority of a content provider should be a string or a reference to a resource. What exactly is the content authority and where is com.android.contacts? Here is the content of the file (w/o license information and comments, API level 16).

<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentAuthority="com.android.contacts"
    android:accountType="com.example.android.samplesync"
    android:supportsUploading="false"
    android:userVisible="true"
/>

Answer

jcwenger picture jcwenger · Mar 2, 2011

There are two basic methods you can use when making a SyncAdapter:

  1. Fill data into an existing ContentProvider.
  2. Create your own ContentProvider to store a new kind of data.

The former is what's going on in this example app. They have some website that has a list of contacts, and they want to store those along with the other contacts on the device. In either case, the way this all works is through a relationship between three components:

  1. A ContentProvider, which stores the data.
  2. A SyncAdapter, which communicates with a remote server to obtain data to put into the ContentProvider.
  3. The Android ContentResolver, which figures out how to pair up SyncAdapters and ContentProviders.

An Android device can have many different ContentProviders and many different SyncAdapters. Since a ContentResolver may not be part of the same .apk as a SyncAdapter, ContentResolver is a system service that finds the right ContentProvider to store a given kind of data. It does this using the ContentAuthority string, which uniquely identifies one specific ContentProvider. Moreover, each ContentProvider must be declared in AndroidManifest.xml which ensures that it can be found by the ContentResolver. Within this declaration you can specify whether the ContentProvider can be used by other applications, see: android:exported.

<provider
    android:name=".CustomProvider"
    android:authorities="com.example.app.provider"
    android:exported="false"
    android:multiprocess="true" >
</provider>

In this case, using an existing ContentProvider, you will need to look at the platform documentation to see what ContentAuthority string they use, and use the same string. If you're creating your own ContentProvider, you just need to ensure that the ContentAuthority you create is unique. The best way to do this is to use parts of your domain name (java class style) in the Authority. Write them in the reverse order. This is illustrated in their example... com.android.contacts.