Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android app to sync data to remote server asynchronously

I am developing an Android app that stores data locally in Sqlite database and sync it to a remote server (MSSQL server). The sending of data is handled through REST api.

This is the way I would like it to work and my plan to handle it:

When the app stores data in Sqlite database, the app will check if internet connection is available, if it is then the app will make a HttpPost to send the data (I use AsyncTask to handle this). Once the data sent, I will flag the row in the database as "synched" using postExecute callback.

If the internet connection is not available, then the app will continue on.

I need to make the app to listen to the event when internet connection became available and then the app will go through all rows that have not been synched and use AsyncTask again to send the data to remote server.

My questions are:

  1. Is it achievable? and if so, is it best practices?
  2. How to listen to the even when internet connection became available?

Thanks,

like image 967
Pelangi Avatar asked Jun 02 '14 22:06

Pelangi


People also ask

What is sync adapter in Android?

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.

What does offline synchronization do in Android?

Offline sync allows end users to interact with a mobile app—viewing, adding, or modifying data—even when there's no network connection. Changes are stored in a local database. Once the device is back online, these changes are synced with the remote backend.

What is Mobile App synchronization?

The Sync apps are available for Android, iPhone and iPad, and make it easy for you to access your files right from your mobile device. The Sync apps are free, and provide the following features: Access your files in Sync from anywhere. Use third-party apps on your device to open and edit files in Sync.


1 Answers

You could implement this manually, but I suggest you use a SyncAdapter instead.

Although you can design your own system for doing data transfers in your app, you should consider using Android's sync adapter framework. This framework helps manage and automate data transfers, and coordinates synchronization operations across different apps. When you use this framework, you can take advantage of several features that aren't available to data transfer schemes you design yourself.

If you want to implement this without using a SyncAdapter anyway, then for the "detect when connection becomes available", you need to add a BroadcastListener to listen for CONNECTIVITY_ACTION broadcasts, then use a ConnectivityManager to query about the current state.

like image 105
matiash Avatar answered Sep 23 '22 13:09

matiash