Samsung Electronics logo

Receiving Notifications on Contact Changes



To create engaging applications with various contacts features, you must learn to receive notifications when contacts are added, updated, or removed:

  1. Define the needed variables:

    /* Watcher identifier */
    var watcherId = 0; 
    
    /* This example assumes that the address book is initialized */
    var addressbook; 
    
  2. Define the event handlers for different notifications using the AddressBookChangeCallback listener interface:

    var watcher = 
    {
       /* When contacts are added */
       oncontactsadded: function(contacts) 
       {
          console.log(contacts.length + " contacts were added");
       },
    
       /* When contacts are updated */
       oncontactsupdated: function(contacts) 
       {
          console.log(contacts.length + " contacts were updated");
       },
    
       /* When contacts are deleted */
       oncontactsremoved: function(ids) 
       {
          console.log(ids.length + " contacts were deleted");
       }
    };
    
  3. Register the listener to use the defined event handlers:

    watcherId = addressbook.addChangeListener(watcher);
    
  4. To stop the notifications, use the removeChangeListener() method of the AddressBook interface:

    function cancelWatch() 
    {
       addressbook.removeChangeListener(watcherId);
    }