5

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

  • If your question has been answered, or if it is no longer valid, please 'tick' to choose the most appropriate answer so everyone knows that the problem has been resolved. Thanks. – wattostudios May 14 '12 at 13:37
  • 1
    @user1017110, Please share your sample code as I want same. I have gone through many examples but nothing helped me. Thank you! – Gangadhar Nimballi Sep 09 '15 at 07:39

1 Answers1

8

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.

wattostudios
  • 8,666
  • 13
  • 43
  • 57
  • Thanks. I managed to send the contacts as json data to the server. So the server receives something like this: {"phone": "2338766745", "contacts": [{"name":"Mike", "telnumber":"18765434567"] }. This works but has to be triggered by the app manually and i think it's not as smooth as what you have suggested. Thanks again. –  Jul 15 '12 at 14:48