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 extends LinkedContactInfoProvider and register the class with your application. For more information, see Create an information provider for a linked contact.
  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 = 
  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;
        contacts = (BlackBerryContactList) PIM.getInstance().
    catch(PIMException e)
        ContactLinkingDemo.errorDialog("Couldn't open contacts list.");   
    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.
  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 Contact Linking Demo sample application that is included in the BlackBerry® Java® SDK.

Next topic: Remove a link

Was this information helpful? Send us your comments.