Android SyncAdapter can be hard for beginners. Here is small example project
Even if you have no plans of using Authentication in your app, you MUST have Authenticator implementation. It can be dummy (stub) but it has to be there.
You will create it in 4 simple steps:
- Extend AbstractAccountAuthenticator and implement empty methods
- Create service that will run implementatuion of Authenticator: AuthenticatorService
- In "xml" directory in your "res" create authenticator.xml with following content
<?xml version="1.0" encoding="utf-8"?>
<account-authenticator xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="com.eutechpro.syncadapterexample"
android:icon="@drawable/ic_launcher"
android:smallIcon="@drawable/ic_launcher"
android:label="@string/app_name" />
Please, pay attention to **android:accountType="com.eutechpro.syncadapterexample"**
- Define StubAuthenticatorService in AndroidManifest.xml file
<service android:name="com.eutechpro.syncadapterexample.StubAuthenticatorService">
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator" />
</intent-filter>
<meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticator" />
</service>
Again, the same story as for Authenticator. Even if you do not need ContentProvider, you MUST have one. Steps to have it:
- Extend ContentProvider and implement empty methods
- Define ContentProvider i AndroidManifest.xml as any other CntentProvider
<provider
android:name="com.eutechpro.syncadapterexample.StubContentProvider"
android:authorities="com.eutechpro.syncadapterexample.provider"
android:exported="false"
android:syncable="true"/>
Here, pay attention to authorities value: android:authorities="com.eutechpro.syncadapterexample.provider"
Now, we are getting to the real deal. Create SyncAdapter extending AbstractThreadedSyncAdapter. Method onPerformSync will be called upon successfull sync call, so there should be your magic code.
But, that is not all. You need few more steps.
First, add xml settings. Afain, in "xml" directory add syncadapter.xml with following code:
<?xml version="1.0" encoding="utf-8"?>
<sync-adapter xmlns:android="http://schemas.android.com/apk/res/android"
android:accountType="com.eutechpro.syncadapterexample"
android:allowParallelSyncs="false"
android:contentAuthority="com.eutechpro.syncadapterexample.provider"
android:isAlwaysSyncable="true"
android:supportsUploading="false"
android:userVisible="true" />
Pay attention on
android:accountType="com.eutechpro.syncadapterexample"
android:contentAuthority="com.eutechpro.syncadapterexample.provider"
For the rest of attributes, please refer to documentation
And final step is to create Service that will run your SyncAdapter: SyncAdapterService.
Of course, define it in Manifset file
<service android:name="ch.teleboy.sync_app_settings.SyncAdapterService"
android:exported="true"
android:process=":sync">
<intent-filter>
<action android:name="android.content.SyncAdapter" />
</intent-filter>
<meta-data
android:name="android.content.SyncAdapter"
android:resource="@xml/syncadapter" />
</service>
Starting Sync process is simple.
- Define account. It doesn't metter if you already created same account
public static final String AUTHORITY = "com.eutechpro.syncadapterexample.provider";
public static final String ACCOUNT_TYPE = "com.eutechpro.syncadapterexample";
public static final String ACCOUNT = "dummyaccount";
Account newAccount = new Account(ACCOUNT, ACCOUNT_TYPE);
accountManager.addAccountExplicitly(newAccount, null, null)
- Start SyncAdapter
ContentResolver.setSyncAutomatically(newAccount, AUTHORITY, true);
- If you want to force Sync process, call
Bundle bundle = new Bundle();
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
ContentResolver.requestSync(newAccount, AUTHORITY, bundle);
At the and, add permissions in manifest
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
<uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />