07-17-2008 09:07 AM
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
07-17-2008 09:22 AM
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.
07-17-2008 10:08 AM
07-17-2008 10:07 PM
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(); }
07-17-2008 10:47 PM
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
.
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.
07-23-2008 08:18 PM
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.
08-21-2009 01:04 AM
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.
09-18-2009 05:39 AM - edited 09-18-2009 05:40 AM
String name = new PhoneCallLogID(number).getName();
09-22-2009 11:41 AM
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.
03-19-2010 06:52 PM
This works really cool!!!!!!!!
Thanks,
Sarthak