Link a contact

You can link a contact in your application with a contact in the contacts application on the BlackBerry® device. You can decide how to interact with the BlackBerry device user when you link a contact. The steps that follow describe one possible approach.
Before you begin:
  • Create the contacts in your application by creating and instantiating a class that implements the LinkableContact interface.
  • Define a class that implements AddressBookFieldFactory and register the class with your application. For more information, see Create a custom field for linked contacts.
  1. Import the required classes and interfaces.
    import net.rim.blackberry.api.pdap.*;
    import net.rim.blackberry.api.pdap.contactlinking.*;
    import javax.microedition.pim.*;
  2. Check to see whether there is a contact in the contacts application (a BlackBerryContact object) that is a linking candidate for your contact. The LinkedContactUtilities.getContactLinkCandidate() method returns a BlackBerryContact if it finds a contact in the contacts application that has the same email address or phone number as the LinkableContact that is passed to it. If there is no matching contact, the method returns null.
    BlackBerryContact bbContact = 
        LinkedContactUtilities.getContactLinkCandidate(linkableContact);
  3. If a match is found, link your contact with the BlackBerryContact.
    bbContact = LinkedContactUtilities.linkContact(bbContact, linkableContact);
  4. If a match is not found, have the user select a contact in the contacts application to link to the selected contact.
    BlackBerryContactList contacts = null;
    try
    {
        contacts = (BlackBerryContactList) PIM.getInstance().
            openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE);
    }
    catch(PIMException e)
    {
        ContactLinkingDemo.errorDialog("Couldn't open contacts list.");   
        return;                 
    }
                    
    Object choice = contacts.choose();
    if(choice instanceof BlackBerryContact)
    {
        bbContact = (BlackBerryContact)choice;
    }
  5. Check to see whether the selected contact in the contacts application is already linked to a contact in your application.
    LinkedContactUtilities.isContactLinked(bbContact, 
        linkableContact.getApplicationID())
    
  6. If the selected contact in the contacts application is not already linked, link it to the contact in your application.
    bbContact = LinkedContactUtilities.linkContact(bbContact, linkableContact);
    

Code sample: Linking a contact

To see an example of this approach to linking a contact, see linkContact() in the sample application for contact linking. To download the sample application, visit www.blackberry.com/go/contactlinkingsample.

Next topic: Remove a link

Was this information helpful? Send us your comments.