7

I have registered a ContentObserver from service and I get the onchange() function when there is update in phone like call or contact update. But I want the onchange() function to be called only when add, update or delete happens. But I don't want when call is incoming or outgoing. So can anybody tell me which URI I can register in contentObserver? My code is here

getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true,new Contact_change());

and Contact_change.java class is like

public class Contact_change extends ContentObserver{

  public Contact_service() {
    super(null);
  }

  @Override
  public void onChange(boolean selfChange){
    Log.i("contact_service","onchange");
    Super.onChange(selfChange);
   }

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

}

Edit:
I have one more problem is that after stop the service if I made change in contact then also onchange() function is called. So how can I stop that or un register the contentobserver.

midhunhk
  • 5,560
  • 7
  • 52
  • 83
Nency
  • 482
  • 1
  • 8
  • 21
  • i don't think this is possible. the time of last call is a field of the Contact, therefore changing it is changing the Contact – njzk2 Sep 03 '12 at 08:17
  • Is it ant other way for get notification of changes in contact?Because i want to synchronize the contacts with my application. – Nency Sep 03 '12 at 08:19
  • I don't think it is really a problem if you receive extra notifications for event you don't need. – njzk2 Sep 03 '12 at 08:23
  • i have problem because even if the contact is not updated but call is incoming or outgoing and the onchange() is called then also i consider as contacts are updated and then i will write all contacts from phone to database.So it is worth case. – Nency Sep 03 '12 at 08:26
  • but it doesn't do any harm to your application. – njzk2 Sep 03 '12 at 08:28
  • it will not harm my application but the performance will be decrease bcz if 1000 contacts are store from phone to database then it will take 5 minutes..and end user point of view it will worth to stay idle in that case – Nency Sep 03 '12 at 08:35
  • then you should consider not storing contacts in your own database. what reason do you have to? – njzk2 Sep 03 '12 at 08:37
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/16180/discussion-between-nency-and-njzk2) – Nency Sep 03 '12 at 08:40

2 Answers2

0

I used the ContactsContract.Contacts.CONTENT_VCARD_URI as mentioned here.

And also you could set a threshold_time like the one mentioned here

Its a bit more efficient.

Community
  • 1
  • 1
madlymad
  • 6,367
  • 6
  • 37
  • 68
0

In order to stop receiving notifications from ContentObserver, you must unregister it.

Create an instance of ContentObserver which you can use later to register/unregister.

Contact_change changeObserver = new Contact_change();

Register observer:

getContentResolver().registerContentObserver(ContactsContract.Contacts.CONTENT_URI, true,changeObserver);

Unregister observer:

getContentResolver().unregisterContentObserver(changeObserver);
Akos
  • 82
  • 3