54

I am trying to listen for any change in the contact database.

So I create my contentObserver which is a child class 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 contact database, My onChange() does not get called.

Mansukh Ahir
  • 3,373
  • 5
  • 40
  • 66
hap497
  • 154,439
  • 43
  • 83
  • 99
  • 3
    FYI - the URI People.CONTENT_URI is deprecated (prior to today - 3/15/2011). Refer to ContactsContract.Contacts.CONTENT_URI. – mobibob Mar 16 '11 at 00:57
  • 1
    Hi,Any ideas on how do i get info on the nature of the change? Contact added, deleted, updated, the fields updated? – Alex Poke Aug 26 '13 at 08:56

2 Answers2

54

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

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);
        MyContentObserver contentObserver = new MyContentObserver();
        getApplicationContext().getContentResolver().registerContentObserver(
            ContactsContract.Contacts.CONTENT_URI, 
            true, 
            contentObserver);
    }

    private class MyContentObserver extends ContentObserver {
        public MyContentObserver() {
            super(null);
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            Log.d(this.class.getSimpleName(), "A change has happened");
        }
    }
}

Something else must be wrong...

Are you making the changes through the cursor the observer is registered with?

Check that with the Observer function deliverSelfNotifications(). (it returns false by default)

You may want to override that observer function with something like:

@Override
public boolean deliverSelfNotifications() {
    return true;
}
technoplato
  • 3,293
  • 21
  • 33
MannyNS
  • 4,703
  • 2
  • 22
  • 16
  • 1
    Strange that it works with no Handler for you. It didn't for me. Moreover, I really wonder how you can get your code executed on the UI Thread... I may be missing something though, I am new to this topic. – Snicolas May 21 '13 at 04:57
  • How can I know that contacts have been updated in Addressbook – user366584 Apr 02 '14 at 07:31
  • Are there any Events that corresponds to Contacts changes to include them into manifest file to not to hold my app's service in memory? – kolyaseg Nov 23 '15 at 08:11
  • Warning!!! People.CONTENT_URI is deprecated use ContactsContract.Contacts.CONTENT_URI instead. – David Sep 20 '17 at 10:11
  • 3
    `onChange()` is calling multiple time on content change.how to prevent this ? – Sagar May 07 '18 at 06:18
28

EDIT: MannyNS's answer has now been updated with the new URI (ContactsContract.Contacts.CONTENT_URI)

A simple TIP about MannyNS 's answer.

Here, People.CONTENT_URI is deprecated.

Code as follows instead.-->ContactsContract.Contacts.CONTENT_URI

    getApplicationContext().getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true, contentobserver);
technoplato
  • 3,293
  • 21
  • 33
Nizam
  • 5,698
  • 9
  • 45
  • 57
  • 5
    In this ContentObserver; Is there a way to get details of contact which added or changed ?? I want to create a similar listener but want to perform some operation on added/modified contact. – Allzhere Oct 01 '13 at 15:58
  • As far as I know, it is not possible using `CotentObserver`. Refer to this [link](http://stackoverflow.com/a/10432259/2124004) – Nizam Oct 02 '13 at 09:22
  • I don’t think this should be a separate answer. I think you should have edited the original to add the deprecation clarification. – technoplato Oct 06 '19 at 11:18