Skip to content

Repository files navigation

SyncAdapterExample

Android SyncAdapter can be hard for beginners. Here is small example project

Steps

1. Create Stub Authenticator

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:

  1. Extend AbstractAccountAuthenticator and implement empty methods
  2. Create service that will run implementatuion of Authenticator: AuthenticatorService
  3. In "xml" directory in your "res" create authenticator.xml with following content
<?xml version="1.0" encoding="utf-8"?>
<account-authenticatorxmlns: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"**
  1. Define StubAuthenticatorService in AndroidManifest.xml file
<serviceandroid:name="com.eutechpro.syncadapterexample.StubAuthenticatorService">
<intent-filter>
<actionandroid:name="android.accounts.AccountAuthenticator" />
</intent-filter>
<meta-dataandroid:name="android.accounts.AccountAuthenticator"android:resource="@xml/authenticator" />
</service>

2. Create Stub ContentProvider

Again, the same story as for Authenticator. Even if you do not need ContentProvider, you MUST have one. Steps to have it:

  1. Extend ContentProvider and implement empty methods
  2. 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"

3. Create SyncAdapter

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-adapterxmlns: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

<serviceandroid:name="ch.teleboy.sync_app_settings.SyncAdapterService"
android:exported="true"
android:process=":sync">
<intent-filter>
<actionandroid:name="android.content.SyncAdapter" />
</intent-filter>
<meta-dataandroid:name="android.content.SyncAdapter"android:resource="@xml/syncadapter" />
</service>

4. Syncing

Starting Sync process is simple.

  1. Define account. It doesn't metter if you already created same account
publicstaticfinalStringAUTHORITY = "com.eutechpro.syncadapterexample.provider";
publicstaticfinalStringACCOUNT_TYPE = "com.eutechpro.syncadapterexample";
publicstaticfinalStringACCOUNT = "dummyaccount";
AccountnewAccount = newAccount(ACCOUNT, ACCOUNT_TYPE);
accountManager.addAccountExplicitly(newAccount, null, null)
  1. Start SyncAdapter
ContentResolver.setSyncAutomatically(newAccount, AUTHORITY, true);
  1. If you want to force Sync process, call
Bundlebundle = newBundle();
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
bundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
ContentResolver.requestSync(newAccount, AUTHORITY, bundle);

5. Add permissions

At the and, add permissions in manifest

<uses-permissionandroid:name="android.permission.AUTHENTICATE_ACCOUNTS" />
<uses-permissionandroid:name="android.permission.READ_SYNC_SETTINGS" />
<uses-permissionandroid:name="android.permission.WRITE_SYNC_SETTINGS" />
<uses-permissionandroid:name="android.permission.AUTHENTICATE_ACCOUNTS" />

5.Enjoy!

About

Android SyncAdapter can be hard for beginners. Here is small example project

Resources

Stars

11 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages