Syncing the phone book contacts in an android device with your application is being used by various android applications today. These applications include Whatsapp, Google+, Skype and various email applications. In this way, contacts syncing allows the user to access contacts from various social applications from the phone book itself.Now I will explain with an example Android application how the syncing process happens. This Android application runs in the background as soon as its launched and synchronizes itself with the device’s phone book contacts. Following is a screenshot of a contact synced with the app.
Clicking on MyData will take us to the desired screen on the application.
Contacts syncing requires three components to work in order which is:-
- Contacts provider
- Authenticator service
- Sync service
Contacts provider is the standard access provider to the device’s data about people. All the contacts in the phone are managed by the contacts provider through tables that can be queried to get data. There are basically three tables that store data for a person.
These tables are-
Figure 1- Contacts provider table structure (Source)
- ContactsContract.Contacts is the table that contains the aggregated data from raw contacts rows that represents different people in the contacts list.
- ContactsContract.RawContacts table contains the person’s accounts and type data summary.
- ContactsContract.Data table contains other details corresponding to each raw contact like email address, phone number, etc.
I made use of the ContentProviderOperation to handle the table insertion operations on the table to add a raw contact and the data corresponding to my app and finally to apply the changes to the database as a batch operation.
Here is the sample code for the class that handles this functionality:
The authenticator service is used to create an account for our app on the device. It plugs into the Android accounts and authentication framework through which an account type is created on the device corresponding to our app. Accounts for standard social applications like Whatsapp, Skype, Facebook, etc. can be found if you visit the accounts screen in settings on your device.
The sync service is made up of two parts the sync adapter and the sync service itself. The sync adapter is used to synchronize data between the server and the local database. My app doesn’t fetch any data from the server but still, it is required to implement the sync adapter. The sync service is what binds the sync adapter to the android sync framework.
The authenticator service runs in the background when the app is launched to create an account for the app on the device. The sync service is called in a periodic cycle to constantly sync the data.
When you tap on the Whatsapp icon on the contacts screen, it will take you to the chat screen of that contact. Similarly, in the phonebook contacts, your app icon can be seen which is clickable to access the app screens that correspond to that contact’s information on the app. The contact data can be sent through the intent extras to the activity that launches.