Retrieve contacts by phone number
You can search all contact lists or a specified contact list on a
BlackBerry® device
for contacts that match a specified phone number.
-
Import the required classes and interfaces.
import net.rim.blackberry.api.pdap.BlackBerryContact;
import net.rim.blackberry.api.pdap.BlackBerryContactList;
import net.rim.blackberry.api.pdap.BlackBerryPIMList;
import net.rim.blackberry.api.phone.Phone;
import java.util.*;
import javax.microedition.pim.PIM;
import javax.microedition.pim.PIMException;
import javax.microedition.pim.PIMItem;
-
Do one of the following:
-
To search all contacts lists, invoke
Phone.getContactsByPhoneNumber(), which returns
a
Vector object that contains the contacts that
match the specified phone number. The
Vector is empty if there are no matching
contacts.
Vector contacts = Phone.getContactsByPhoneNumber(phoneNum);
-
To search a specified contact list, invoke
BlackBerryContactList.itemsByPhoneNumber(),
which returns an
Enumeration object of all contacts that match
the specified phone number. Each of the items in the
Enumeration is a
PIMItem object, which you can cast to a
BlackBerryContactList object.
BlackBerryContactList list = (BlackBerryContactList)
PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE, "test");
Enumeration myEnum = list.itemsByPhoneNumber(phoneNum);
Code sample
try
{
BlackBerryContactList list = (BlackBerryContactList)
PIM.getInstance().openPIMList(PIM.CONTACT_LIST, PIM.READ_WRITE, "test");
Enumeration myEnum = list.itemsByPhoneNumber(phoneNum);
while (myEnum.hasMoreElements())
{
Object o = myEnum.nextElement();
if (o instanceof BlackBerryContact)
{
BlackBerryContact c = (BlackBerryContact) o;
String[] name = c.getStringArray(Contact.NAME, 0);
add(new RichTextField("A matching contact is " +
name[Contact.NAME_GIVEN] + " " + name[Contact.NAME_FAMILY]));
}
}
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
Was this information helpful? Send us your comments.