I have an app in which I am hoping to send details in the android contact list to a remote server, so that the user can see his contacts online. To do this I want to notify the remote server of any changes made on the phone to the contacts list.
I have set up a ContentObserver on the 'ContactsContract.Contacts.CONTENT_URI' from a service that gets started when the phone is booted.
I have a number of quiestions, the first 2 are incidental, the third is my major concern.
1: Once I have set up a service that registers a ContentObserver on my Cursor, does that observer only exist within the service? I mean, if the service is killed, does the contentObserver continue to observe?
2: I suspect the answer is no, but I will ask anyway. Is there anyway of knowing which contact being updated is triggering the onchange method of my contentObserver? currently I have to compile the list of all teh contacts on the phone and send them off to my remote server, it would be so much easier to just send details of the contacts being updated.
3: This is my main question, when I make a change to my Contact List the onChange method is being fired twice in quick succession. 1 change, 2 calls. Is there anyway of managing this?
public class ContactService extends Service {
JSONArray contactList;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
Log.i("C2DM","content observers initialised");
super.onCreate();
//Call Log Content Provider observer
MyContentContactsObserver contactsObserver = new MyContentContactsObserver();
ContactService.this.getContentResolver().registerContentObserver (ContactsContract.Contacts.CONTENT_URI, true, contactsObserver);
}
private class MyContentContactsObserver extends ContentObserver {
public MyContentContactsObserver() {
super(null);
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.i("LOG","detected change in contacts: "+selfChange);
}
}
}
REsults in 2 quick lines in my logCat:
detected change in contacts: false
detected change in contacts: false