String format of phone number (and ContactUtils)

bsnqt

Active Member
Licensed User
Longtime User
Dear Erel,

Using the ContactUtils is much and much faster to find & load all Contacts from phone than using Phone and Contacts2 library. Earlier, I did that task with more than 12 sec. (as also reported by other members), now this task is done very quickly, almost immediately or < 1 sec.

However, I still have a problem of implementing this class. It is related to the phone number format issue which still remains an unanswered question for me.

If I want to find a stored Contact name by using the phone number and "FindContactsByPhone", I have to know exactly the phone number that I stored earlier. Let say in the past I stored "003311223344" now if my searching number is "0033-1122-3344", then I cannot find out this Contact.

I am doing anything wrong? How can we eliminate the "phone number format" factor when doing search, as the user can enter (store) every kind of format they want, let say 0033-1122-3344 or (00) 33 1122- 3344 and so on...

B4X:
Sub Button2_Click
    Dim phoneList As List
    phoneList.Initialize
    phoneList = cu.FindContactsByPhone("0033-1122-3344", False, False)
    For i = 0 To phoneList.Size - 1
        ToastMessageShow(phoneList.Get(i), False)
    Next
End Sub

Currently what I am doing is to "manually" compare the user number and the stored Contact number by removing all the symbolic characters and spaces programmatically, as well as removing the prefix... then I compare their Substring.
 

bsnqt

Active Member
Licensed User
Longtime User
I just found an answer to my question posted previously...

For whom who have interest in looking to how to get a right format of phone number string "automatically", you may need to retrieve the phone numbers from Contacts using ContentResolver library and look for column "data4" when making the query... It will give you automatically so called "normalized" phone numbers with Country code (phone international format)...

http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.Phone.html

public static final String NORMALIZED_NUMBER
Added in API level 16

The phone number's E164 representation. This value can be omitted in which case the provider will try to automatically infer it. (It'll be left null if the provider fails to infer.) If present, NUMBER has to be set as well (it will be ignored otherwise).

Type: TEXT

Constant Value: "data4"

As this post is related to all: String format of a phone number, ContentResolver and ContactUtils I decided to post it here :) Sorry Erel but I don't know where is better to post. ContentResolver is actually very powerful for working with Contacts.

I love this forum as I can learn a lot :)
 
Last edited:
Upvote 0

ipika july

Member
Licensed User
Longtime User
Is only comparing all contacts is solution ? Isnt there any native method to do search ?

Can you post your code which you used.
 
Upvote 0

ipika july

Member
Licensed User
Longtime User
Look at the link: PHONENUMBERUTILS

B4X:
compare(String a, String b)
Compare phone numbers a and b, return true if they're identical enough for caller ID purposes.

yes i saw this thread already but it is to compare two numbers. this can be used by iterating through all contacts.

But i want to know isn't there any method which searches in normalised contact numbers.
 
Upvote 0

ipika july

Member
Licensed User
Longtime User
I don't think you can find a contact without searching and comparing...
Maybe this link is interesting for you:
http://www.b4x.com/android/forum/th...combines-contactsutils-with-searchview.31372/
Ah....Thank you again...I have already seen link.

Interesting thing is...I browsed SQLite database of contacts2.db ..Contact storage.
& There exist a table phone_lookup & it has a column normalized_number. We can use it but only on rooted devices :(

Any idea how to use it without root ?
 
Upvote 0

bsnqt

Active Member
Licensed User
Longtime User
No. I am pretty sure that you can still use ContentResolver to query (to read and compare) without need of rooting the phone. A "normal" user's phone is not rooted.
 
Upvote 0
Top