Note that there are some explanatory texts on larger screens.

plurals
  1. POContent Observer onChange method called twice after 1 change in cursor
    text
    copied!<p>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. </p> <p>I have set up a ContentObserver on the 'ContactsContract.Contacts.CONTENT_URI' from a service that gets started when the phone is booted. </p> <p>I have a number of quiestions, the first 2 are incidental, the third is my major concern.</p> <p>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?</p> <p>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.</p> <p>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?</p> <pre><code> 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); } } } </code></pre> <p>REsults in 2 quick lines in my logCat: </p> <pre><code> detected change in contacts: false detected change in contacts: false </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload