Note: We recommended WorkManager as the recommended solution for most background processing use cases. Please reference the background processing guide to learn which solution works best for you.
The sync adapter component in your app encapsulates the code for the tasks that transfer data between the device and a server. Based on the scheduling and triggers you provide in your app, the sync adapter framework runs the code in the sync adapter component. To add a sync adapter component to your app, you need to add the following pieces:
- Sync adapter class.
- A class that wraps your data transfer code in an interface compatible with the sync adapter framework.
-
Bound
Service. - A component that allows the sync adapter framework to run the code in your sync adapter class.
- Sync adapter XML metadata file.
- A file containing information about your sync adapter. The framework reads this file to find out how to load and schedule your data transfer.
- Declarations in the app manifest.
- XML that declares the bound service and points to sync adapter-specific metadata.
This lesson shows you how to define these elements.
Create a sync adapter class
In this part of the lesson you learn how to create the sync adapter class that encapsulates the data transfer code. Creating the class includes extending the sync adapter base class, defining constructors for the class, and implementing the method where you define the data transfer tasks.
Extend the base sync adapter class
To create the sync adapter component, start by extending
AbstractThreadedSyncAdapter and writing its constructors. Use the
constructors to run setup tasks each time your sync adapter component is created from
scratch, just as you use Activity.onCreate() to set up an
activity. For example, if your app uses a content provider to store data, use the constructors
to get a ContentResolver instance. Since a second form of the
constructor was added in Android platform version 3.0 to support the parallelSyncs
argument, you need to create two forms of the constructor to maintain compatibility.
Note: The sync adapter framework is designed to work with sync adapter components that are singleton instances. Instantiating the sync adapter component is covered in more detail in the section Bind the Sync Adapter to the Framework.
The following example shows you how to implement
AbstractThreadedSyncAdapterand its constructors:
Kotlin
/** * Handle the transfer of data between a server and an * app, using the Android sync adapter framework. */ class SyncAdapter @JvmOverloads constructor( context: Context, autoInitialize: Boolean, /** * Using a default argument along with @JvmOverloads * generates constructor for both method signatures to maintain compatibility * with Android 3.0 and later platform versions */ allowParallelSyncs: Boolean = false, /* * If your app uses a content resolver, get an instance of it * from the incoming Context */ val mContentResolver: ContentResolver = context.contentResolver ) : AbstractThreadedSyncAdapter(context, autoInitialize, allowParallelSyncs) { ... }
Java
/** * Handle the transfer of data between a server and an * app, using the Android sync adapter framework. */ public class SyncAdapter extends AbstractThreadedSyncAdapter { ... // Global variables // Define a variable to contain a content resolver instance ContentResolver contentResolver; /** * Set up the sync adapter */ public SyncAdapter(Context context, boolean autoInitialize) { super(context, autoInitialize); /* * If your app uses a content resolver, get an instance of it * from the incoming Context */ contentResolver = context.getContentResolver(); } ... /** * Set up the sync adapter. This form of the * constructor maintains compatibility with Android 3.0 * and later platform versions */ public SyncAdapter( Context context, boolean autoInitialize, boolean allowParallelSyncs) { super(context, autoInitialize, allowParallelSyncs); /* * If your app uses a content resolver, get an instance of it * from the incoming Context */ contentResolver = context.getContentResolver(); ... }
Add the data transfer code
The sync adapter component does not automatically do data transfer. Instead, it
encapsulates your data transfer code, so that the sync adapter framework can run the
data transfer in the background, without involvement from your app. When the framework is ready
to sync your application's data, it invokes your implementation of the method