How to listen to changes in the contacts database

I am trying to listen for any changes to the contacts database.

So, I create my contentObserver, which is a child of ContentObserver :

  private class MyContentObserver extends ContentObserver { public MyContentObserver() { super(null); } @Override public void onChange(boolean selfChange) { super.onChange(selfChange); System.out.println (" Calling onChange" ); } } MyContentObserver contentObserver = new MyContentObserver(); context.getContentResolver().registerContentObserver (People.CONTENT_URI, true, contentObserver); 

But when I use ' EditContactActivity ' to change the contacts database, My onChange() not called.

+52
android
09 Sep '09 at 18:26
source share
2 answers

I have deployed your example as is and it works great.

 package com.test.contentobserver; import android.app.Activity; import android.database.ContentObserver; import android.os.Bundle; import android.provider.Contacts.People; public class TestContentObserver extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.getApplicationContext().getContentResolver().registerContentObserver (People.CONTENT_URI, true, contentObserver); } private class MyContentObserver extends ContentObserver { public MyContentObserver() { super(null); } @Override public void onChange(boolean selfChange) { super.onChange(selfChange); } } MyContentObserver contentObserver = new MyContentObserver(); } 

So you have to do something else wrong.

Do you make changes through the cursor to which the observer is registered?

Make sure the Observer deliverSelfNotifications () function. (by default it returns false)

You can override this observer function with something like:

 @Override public boolean deliverSelfNotifications() { return true; } 

Make sure People.CONTENT_URI is referencing the correct value (android.provider.Contacts.People).

In addition, I would suggest you use Handler with ContentObserver, although this does not make your code incorrect in this case.

+53
Sep 09 '09 at 23:39
source share

Simple tip on MannyNS answer.

People.CONTENT_URI deprecated here.

Code instead .--> ContactsContract.Contacts.CONTENT_URI

  getApplicationContext().getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contentobserver); 
+28
Jun 03 '13 at 10:04 on
source share



All Articles