Retrieve the contact that is associated with an active call

  1. Import the required classes and interfaces.
    import net.rim.blackberry.api.pdap.BlackBerryContact;
    import net.rim.blackberry.api.phone.*;
  2. Implement a phone listener.
    class MyPhoneListener extends AbstractPhoneListener
    {
    }
  3. Invoke PhoneCall.getContact() in a callback method in the phone listener (for example, in callIncoming() for a new call). The getContact() method searches all contact lists on the BlackBerry® device and returns a BlackBerryContact that is associated with the current phone number, or null if there is no matching contact.
    public void callIncoming(int callId)
    {
       PhoneCall call = Phone.getCall(callId);
       BlackBerryContact contact = call.getContact();
    }
  4. Use the retrieved contact information.

Code sample

public void callIncoming(int callID) // in a phone listener
{
   StringBuffer strBuffer = new StringBuffer();
      
   PhoneCall call = Phone.getCall(callID);        
   BlackBerryContact contact = call.getContact();
            
   if(contact != null)
   {    
      if(contact.countValues(BlackBerryContact.ADDR) > 0)
      {
         String[] strArray = contact.getStringArray(BlackBerryContact.ADDR, 0);
         String city = strArray[BlackBerryContact.ADDR_LOCALITY];
         if(city != null && city.length() > 0)
         {
            strBuffer.append(city);                
         }            
                
         String country = strArray[BlackBerryContact.ADDR_COUNTRY];
         if(country != null && country.length() > 0)
         {
            if(city != null && city.length() > 0)
            {
               strBuffer.append(", ");
            }
                    
            strBuffer.append(country);
         }
      }                         
   }
   // use the contact info, for example, 
   // display it on the incoming phone screen
}
Previous topic: Remove a contact list

Was this information helpful? Send us your comments.