tags:

views:

1582

answers:

2

Hi,

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 function does not get called.

+1  A: 

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);
        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 must be doing something else 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;
    }

Make sure that People.CONTENT_URI is referring to correct value (android.provider.Contacts.People).

Also, I would suggest you using Handler with ContentObserver, though that is not what makes your code wrong in this case.

MannyNS
A: 

how can i detect the CallLog changes

MIke