Android account, sync adapter, and contacts contract database to link to your app
Here we are going to introduce how to create an android account of your web service and support data synchronization, for example the address book synchronization, and make android contacts support your app’s features or functions.
Create an Android account of your service type
A custom account means an account different from the built-in account type, which is Google account(com.google). Every web service can create their own account type, like Facebook, Twitter,…etc, and with their Account Sync Service to download or synchronize data with their server.
Android developers training doc has described how to create a custom account type and provided an example SampleSyncAdapter app for your reference.
Here are quick summaries:
- Create your Authentication Service and Authenticator.
AndroidManifest.xml
my_authenticator.xml
2. Implement your Authenticator with an authenticate activity
This is to handle the event when the user clicks Add account and choose your account type.
addAccount() from AbstractAccountAuthenticator
Create a synchronization service for your account
Then you can provide synchronization for your account by setting your account with synchronize configuration:
// Set this account automatically sync
ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true);
// Set this account periodically sync with the specified interval
ContentResolver.addPeriodicSync(account, ContactsContract.AUTHORITY, Bundle.EMPTY, MY_SYNC_INTERVAL);
Note: Since we are demonstrating how to add app support to contacts, this AUTHORITY here is using ContactsContract.AUTHORITY.
- Create your Account Sync Service and Sync Adapter
AndroidManifest.xml
my_sync_adapter.xml
2. Implement your Account Sync Adapter to perform synchronization
onPerformSync() from AbstractThreadedSyncAdapter
Create your Account MIME Type for Android ContactsContract database
To indicate a contact is actually an account of your service, we need to defined your Account’s Contacts Source and ContactsDataKind in the Sync Service.
AndroidManifest.xml
my_account_contacts_source.xml
The mimeType strings will be used to store a contact data with the type indicating the ‘capability’ of this contact.
For example, a contact with ‘allo’ can indicate the contact is also my ‘allo’ service account, that I can talk to with my ‘allo’ protocol.
Insert a contact data to indicate its supporting service(like ‘allo’ in this above case):
The CALLER_IS_SYNCADAPTER is indicating this operation is from sync adapter.
The DATA1 field is mapping to ContactsContract.CommonDataKinds.Phone.NUMBER, which is usually to store some account id of your service.
The DATA2 field is mapping to ContactsContract.CommonDataKinds.Phone.TYPE , can be the type of this account.
The DATA3 field is mapping to ContactsContract.CommonDataKinds.Phone.LABEL, which is a caption to show in that contact’s option field, which is “Allo Messaging Stephen” in our show case.
Add your Account MIME Type into the handler Activity
With all these above, you will still not succeed to show your custom field into the contact detail. Because it requires to defined an Activity to take care of this intent MIME type.
AndroidManifest.xml
How to make your account contact data Read-Only
When you provide the synchronization to sync your account contacts into Android ContactContract Database, those contacts data could be displayed in the Android Phone Book application and the user can modify. But you might want to make your account’s contact data read-only to the user, i.e. can not modify. Then you just indicate through your sync adapter xml:
android:supportsUploading="false"