Android SyncAdapter sync Local to Web Server Demo

Android have its own implementation to let local database to synchronize with the web server which using SyncAdapter. The SyncAdapter able to synchronize between local database and web server to make both have the same database. Why you want synchronize? it is able to let the user backup their local database in the web server so they will not lost the important data in the application. Besides that, user also able to use the application without network connection after synchronize the latest database. By using SyncAdapter, you can set how long u want to synchronize the database. In this tutorial, I will teach you how to use android syncadapter in the application, this application wont do any local database and web server synchronize. This is just let you have basic concepts on it.

Creating a New Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “SynAdapterExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Activity” and Click Next button
5. Lastly, press finish button.

Create a AbstractThreadedSyncAdapter class

Add a new class and name it to SyncAdapter in your java package and this class will extends the AbstractThreadedSyncAdapter object to perform sync in the application.

Create a ContentProvider class

Add a new class for contentprovider name as SyncProvider, this is a dummy contentprovider without any database. This is to easy for demonstrate the feature. You can add your SQLite to the ContentProvider in the following link. https://questdot.com/android-contentprovider/

Create an Authentication account Service

This service is to authenticate the account in the application so you can deny unauthentication account to use this application. The following source code are the sample of implementation.

Add xml package and create a new xml for account

Create a xml account layout for authentication account. The account type of following code should same as GenericAccountService class account type.

Create Sync Service

Add a new class for synchronize service and it will be bind with the syncadapter. You can copy the source code.

Create a new xml file in xml folder

Create a xml for sync service, you can follow the source code in the bottom. AccountType should be same and contentAuthority is same as contentProvider in your SyncProvider class.

Create a Application class

Add a new class in your java package. This application class to initialize the new account in your application. It is dummy and without authentication.

Edit activity_main.xml layout

Edit your activity_main layout and this layout i will add the button to perform sync in the application.

Edit MainActivity.java class

Go to your mainactivity class and copy paste the following code in this class to perform sync.

Edit AndroidManifest.xml

I will add four permmissions in the androidmanifest to perform sync which you can see in the source code below. Also add the components in the manifest file, you can see the example below.

Edit strings.xml

Run your project

Finally, you can try how the synchronize work in the sample project. Note : check the log in the Android Monitor to understand how its work.

(Android SyncAdapter sync Local to Web Server)

Source Code

(Visited 1,981 times, 1 visits today)
Advertisements

Yong Loon Ng

Ng Yong Loon, better known as Kristofer is a software engineer and computer scientist who doubles up as an entrepreneur.

You may also like...

1 Response

  1. CHEFOUETMEKA says:

    Merci pour ce Post. J’ai quelques question :
    1- comment recuperer les donnee du Thread principale dans notre fonction public void onPerformSync(Account account,
    Bundle extras,
    String authority,
    ContentProviderClient provider,
    SyncResult syncResult) ?

Leave a Reply

Your email address will not be published. Required fields are marked *