Udo
Udo

Reputation: 1157

Listen to chages in contacts database from background service on android device

i am developing an app with phonegap 2.2. I have been able to implement a background service from https://github.com/Red-Folder/Cordova-Plugin-BackgroundService.

right now i want to be able to listen for changes in the contacts database such that when a change is detected, a function can run in the background service (Java Method). How can this be implemented.

Upvotes: 0

Views: 1083

Answers (1)

Akos
Akos

Reputation: 82

You have to create a content observer in your service and register that observer to listen for changes in contacts database. Here is an example of contacts content observer:

ContactsContentObserver contentObserver = new ContactsContentObserver();
private class ContactsContentObserver extends ContentObserver
{
    public ContactsContentObserver()
    {
        super(null);
    }

    @Override
    public void onChange(boolean selfChange)
    {
        super.onChange(selfChange);

        // handle change received
    }
}

You can register the content observer in service onStart() method:

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

And unregister it in service onDestroy() method:

getContentResolver().unregisterContentObserver(contentObserver);

Hope it helps.

Upvotes: 1

Related Questions