automatically add/synchronize contacts

android, java, whatsapp, xmpp

Solution

If your contact sync is to run separately from your chat app, you should use a `SyncAdapter`. This runs as an Android `service`, so it can keep the contacts in-sync with a server even when your chat app isn't running. See the following URL for the `SampleSyncAdapter` provided by Google, that gives source code and information for building your own `SyncAdapter`... http://developer.android.com/resources/samples/SampleSyncAdapter/index.html

If you're only wanting to sync contacts when your app is running, then you don't actually need any kind of sync mechanism, you just need to add contacts to your device.

For both these methods, you add contacts by creating `ContactsContract` objects, writing them to the Android contacts database. There are a number of variants of `ContactsContract` depending on the type of data you want to store - for example, `ContactsContract.CommonDataKinds.Email` is used to store email addresses for a contact. See this URL for information on `ContactsContract`... http://developer.android.com/reference/android/provider/ContactsContract.html

I would recommend that you definately read through the source code of the `SampleSyncAdapter` http://developer.android.com/resources/samples/SampleSyncAdapter/index.html as it provides all the code you need to read and write contacts from the Android contacts database, it shows how to build the `ContactsContract` for storing contact information, and provides an example of the `SyncAdapter` if you choose to use it.

Problem

I am developing an android chat app and would like to automatically add/synchronize contacts like whatsapp does when the user installs the app, and whenever a new contact is added or deleted. I intend using xmpp or a java library for the app. Thanks

Original source