0

How to get notification into android application, when there is a change (addition, updatation, deletion) in device contact?

These notification needed along-with the changes in following two scenarios:

  1. When app is running.
  2. App is not running and there are changes in the device contacts. Next time when app starts, app should receive notification with the changes done when app was not running.
Thanh-Nhon Nguyen
  • 3,402
  • 3
  • 28
  • 41
  • http://stackoverflow.com/questions/1401280/how-to-listen-for-changes-in-contact-database, http://www.grokkingandroid.com/use-contentobserver-to-listen-to-changes/ – ramizmoh Jul 11 '14 at 09:24

1 Answers1

0

For the first question: Use a Content Observer attached to People storage:

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

Where:

class ContactOnserver extends ContentObserver {

        public MyContentObserver() {
            super(null);
        }

        @Override
        public void onChange(boolean selfChange) {
            // handle change here <----------------------------
        }

    }

This will keep you notified on Contact changes while your app is running.


As for your other question - how to get all changes made when app was NOT running - do something like this:

A. Before going down, have your app record the current time into a SharedPreferences field

B. After next load, query Contacts table for changes made after last-exit time. I'm not providing any code sample here but I'm sure you will manage. Search the web for something like:

  ContentResolver cr = getContentResolver();
  Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
Gilad Haimov
  • 5,767
  • 2
  • 26
  • 30