How do I use the Android SyncAdapter?

Viewed 48739

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"
/>
3 Answers

When your APK gets loaded, the directives in the manifest tell the OS to review all meta-data. In this case, it is content meta data for Android contacts. The name that is used to find the provider is com.android.contacts (defined by Android) -- and the owner has the "authority" to provide the content access to its data base (i.e., the ContentProvider).

N.B. You could be a content provider of your own data that can be sync'ed with your web services that is not contacts. The meta-data is a mechanism to register with the OS so you can find it during a broadcast message.

This defined meta-data is going to be associated with your SyncAdapter by virtue of the 'sync type' that you provide. The name of the type is the android:accountType and might be 'com.mycompany.myapp'. That key is used during a broadcast to all sync adapters and your coded BroadcastReceiver will handle the message with your type.

That is the start of the relationships and some breakdown of the terminology.

Related