Android - Sync local Sqlite database to a remote SQLServer Database.
I have an android application which allows user to enter data and save it to locals device and the data needs to be uploaded periodically to remote SQL Server.
As the first step I developed a contentprovider to wrap the sqlitedb in the first application which was laready operationsl. As I already had a database handelr which extends SQLiteOpenHelper for the application. I developed the content provider with lots of dummy overrides and I kept using SQLiteOpenHelper for my first application. Only interesting area for the first application is its manifest file where the Authority and permissions are defined. Defining this took lots of time as I had to do lots of trial and error.
android:authorities="in.xxx.xxx.provider"
android:exported="true"
android:grantUriPermissions="true"
android:readPermission="android.permission.permRead" />
First provider has to be within application tags.
androidname is the file name of my provider class attached with the package name.
auhtoritis is the AUTHORITY declared inside the provider class.
Relevant portion of the provider class is
//from vogela http://www.vogella.com/articles/AndroidSQLite/article.html
private static final String AUTHORITY = "in.xxx.xxx.provider";
private static final String BASE_PATH = "members";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "/" + BASE_PATH);
At this point I had my first application running. This application is responsible for all database operations. Now I need to develop another application which allows me to sync data periodically.
Little bit research and help from Stackoverflow shows that the best approach is to use SyncAdapter. Sync Adapter has three componenets Account, Authentication and SyncAdapter.
Important links
Breaking down Google’s SampleSyncAdapter
How to run Sync Immediately
Reg selection of Records to be updated from Server and vice versa
Idea about Authentication
Important resources
I have an android application which allows user to enter data and save it to locals device and the data needs to be uploaded periodically to remote SQL Server.
As the first step I developed a contentprovider to wrap the sqlitedb in the first application which was laready operationsl. As I already had a database handelr which extends SQLiteOpenHelper for the application. I developed the content provider with lots of dummy overrides and I kept using SQLiteOpenHelper for my first application. Only interesting area for the first application is its manifest file where the Authority and permissions are defined. Defining this took lots of time as I had to do lots of trial and error.
android:exported="true"
android:grantUriPermissions="true"
android:readPermission="android.permission.permRead" />
First provider has to be within application tags.
androidname is the file name of my provider class attached with the package name.
auhtoritis is the AUTHORITY declared inside the provider class.
Relevant portion of the provider class is
//from vogela http://www.vogella.com/articles/AndroidSQLite/article.html
private static final String AUTHORITY = "in.xxx.xxx.provider";
private static final String BASE_PATH = "members";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "/" + BASE_PATH);
At this point I had my first application running. This application is responsible for all database operations. Now I need to develop another application which allows me to sync data periodically.
Little bit research and help from Stackoverflow shows that the best approach is to use SyncAdapter. Sync Adapter has three componenets Account, Authentication and SyncAdapter.
Important links
Concept of SyncAdapter (android.content.AbstractThreadedSyncAdapter)
Connecting The Dots with Android SyncAdapterBreaking down Google’s SampleSyncAdapter
How to run Sync Immediately
Reg selection of Records to be updated from Server and vice versa
Idea about Authentication
Important resources
Looking back again at AndroidManifest, that strange meta-data tag in our service is the key piece that establishes the binding between a ContentAuthority and an account. It externally references another xml file (call it whatever you like, something relevant to your app.) Let's look at sync_myapp.xml:
xml version="1.0" encoding="utf-8" ?>
xmlns:android="http://schemas.android.com/apk/res/android"
android:contentAuthority="com.android.contacts"
android:accountType="com.google"
android:userVisible="true" />
Okay, so what does this do? It tells Android that the sync adapter we've defined (the class that was called out in the name element of the
tag that includes the tag that includes this file...) will sync contacts using a com.google style account.
All your contentAuthority strings have to all match, and match with what you're syncing -- This should be a string you define, if you're creating your own database, or you should use some existing device strings if you're syncing known data types (like contacts or calendar events or what have you.) The above ("com.android.contacts") happens to be the ContentAuthority string for contacts type data (surprise, surprise.)
accountType also has to match one of those known account types that are already entered, or it has to match one you're creating (This involves creating a subclass of AccountAuthenticator to get auth on your server... An article worth itself.) Again, "com.google" is the defined string identifying... google.com style account credentials (again, this should not be a surprise.)
No comments:
Post a Comment