Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
javec
Posts: 445
Registered: ‎07-15-2008
My Device: Not Specified

Retreive Name from Phone Number

Hi,

if I enter a number (phone number) into a field in my application, is there a way to get the name associated with that number in the address book? 

 

So lets say my field contains the number 0115742222 and in my address book on the device, there is a contact with that phone number, is there a way to display the name to that number?

 

It is not important if the number entered in the field is the mobile number or home number.

 

Thank you 

 

If your question was answered, please accept it as a sollution and / or give Kudos ;-)
______________________________
Use the Search Field first! Maybe the question you want to ask already exists!
Please use plain text.
Developer
kzettel
Posts: 120
Registered: ‎07-16-2008
My Device: 9000

Re: Retrieve Name from Phone Number

It is possible.  You can lookup the list of contacts using the PIM class and some of the other classes in the javax.microedition.pim package.  For examples of working with the address book there is a PIMDemo application bundled with the jde, a section in the BlackBerry JDE development guide about working with contacts, and a knowledge base article called How To Access Address Book Contacts.

 

I don't believe there are any APIs to search by phone number but you can iterate through each contact and see if they have the phone number that was entered. 

 

Kurt Zettel | Chief Architect | Metova
www.metova.com
Please use plain text.
BlackBerry Development Advisor (Retired)
bzubert
Posts: 86
Registered: ‎07-11-2008
My Device: Not Specified

Re: Retrieve Name from Phone Number

To match a contact against the phone number, open the contact list via a call to PIM.openPIMList(int pimListType, int mode), then call PIMList.items(String matching) to find all contacts whose phone number match the number you provided.  Note that formatting of the phone number you have may be the most difficult part of getting a match.  You'll likely either need to validate the user's phone number format (ensuring consistency with how that user has been storing phone numbers in their contact list), or you may want to try calling items() with pieces of the phone number (such as the last 4 digits), getting matches that way.
Brian Zubert
Technical Partnership Manager
Research In Motion
Please use plain text.
New Developer
leisuresuit
Posts: 4
Registered: ‎07-16-2008
My Device: Not Specified

Re: Retreive Name from Phone Number

We also wanted a reverse-lookup feature but gave up on it due to performance issues. The code below works, but BlackBerryContactList.items is too slow (takes 5 seconds per lookup with ~200 entries).

 

 

PIM pim = PIM.getInstance(); String[] lists = pim.listPIMLists(PIM.CONTACT_LIST); for (int i = 0; i < lists.length; i++) { BlackBerryContactList list = (BlackBerryContactList)pim.openPIMList(PIM.CONTACT_LIST, PIM.READ_ONLY, lists[i]); for (Enumeration e = list.items(number, BlackBerryContactList.SEARCH_CONTACTS); e.hasMoreElements(); ) { Contact contact = (Contact)e.nextElement(); }

 

 

 

Please use plain text.
Developer
richard_puckett
Posts: 191
Registered: ‎04-03-2008
My Device: Bold 9700

Re: Retreive Name from Phone Number

I recently had a tester who had over 1200 contacts in his addressbook (I subsequently made a little app that stuffed 1200 contacts into my test device so I could duplicate their environment.  Darn testers always making my life difficult  :smileyhappy:.

 

What I did was just run through the contact list and create my own AddressBook model containing only the fields I'm interested in.  I then used that small memory-resident version to search and find fields based on an input (phone number, contact name, etc) which was "performant" enough at that point to send into production.

 

Searches were asynchronous and the caller would be notified if the AddressBook was in the process of loading or if there was data available, etc.

Please use plain text.
New Developer
singalen
Posts: 2
Registered: ‎07-23-2008
My Device: Not Specified

Re: Retrieve Name from Phone Number


bzubert wrote:
Note that formatting of the phone number you have may be the most difficult part of getting a match.  You'll likely either need to validate the user's phone number format (ensuring consistency with how that user has been storing phone numbers in their contact list), or you may want to try calling items() with pieces of the phone number (such as the last 4 digits), getting matches that way.

Oh.

My customer has about 3 thousands of contacts on his quite new Pearl. I can't guarantee that he formats all the phones same way.

Even one call to items(something) takes quite long (now about 3 minutes for several calls), 4-6 calls for each contact will take ages.

And after all I'm not GUARANTEED to find the contact.

I wish in future ROMs there will be an items(PIMComparator) method... and even then all the poor backward-compatible guys will need to wait for another five years.

Oh yeah, I also wish for BlackberryContactList.choose() for phones, and with "use once" option.

 

Sorry. I feel bitter.

 

As a workaround, looks like I'll need to create an own copy of address book, with phone numbers in some "canonical" form, and have proper synchronizationion issues.

Please use plain text.
New Developer
pan_magically
Posts: 8
Registered: ‎10-14-2008
My Device: Not Specified

Re: Retrieve Name from Phone Number

For 1000 contacts on a 8820(4.5.0 OS) real device, I read all the contacts then do indexing. The result is slow due to the casting of contact to BlackberryContact.

 

 

long start = DateUtil.getNow();
Enumeration items = this.bb_contact_list.items(BlackBerryContactList.SEARCH_CONTACTS);
fetchTime = DateUtil.getNow() - start;

for(; items.hasMoreElements();){
start = DateUtil.getNow();
BlackBerryContact contact = (BlackBerryContact)items.nextElement();
castingTime += (DateUtil.getNow() - start);

start = DateUtil.getNow();
this.addContact(contact); // do some index in this method
addTime += (DateUtil.getNow() - start);

.

.

.

 

After the loop finished, the time taken is as follows.

 

fetchTime=20ms
castingTime=22000ms
addTime=6496ms

 

This will be hard to avoid since the bottleneck is the casting part.

 

Please use plain text.
Developer
oliver_kranz
Posts: 216
Registered: ‎09-18-2009
My Device: Z10
My Carrier: O2

Re: Retrieve Name from Phone Number

[ Edited ]

String name = new PhoneCallLogID(number).getName();

 

Message Edited by oliver_kranz on 18-09-2009 11:40 AM
Please use plain text.
Developer
oliver_kranz
Posts: 216
Registered: ‎09-18-2009
My Device: Z10
My Carrier: O2

Re: Retrieve Name from Phone Number

Something I wanted to note about the code above.

 

It has the advantage of being very efficient concerning execution speed. Furthermore, it is even possible to add special characters like spaces or parenthesis to the phone numbers to look up. If the passed phone number and the phone number in the address book differ in these special characters they also match and the name still is returned.

Please use plain text.
Contributor
sarthak
Posts: 12
Registered: ‎02-18-2010
My Device: 8250
My Carrier: T-mobile

Re: Retrieve Name from Phone Number

This works really cool!!!!!!!!

 

Thanks,

 

Sarthak

Please use plain text.