Android Question Phone number formating

drenwick

New Member
Licensed User
Hi all,
I am having trouble working out how to handle phone numbers. I am getting both call and inbound sms notifications and note that the different processes deliver differently formatted numbers. For example SMS MessageReceived delivers an international formatted number +44754xxxx and the PhoneStateChanged event has an incoming number without the country code 0754xxx.
I have to match these numbers against a list of numbers that is made up of different strings +44 754xx, or +44754xxx, 0044754xxxx, +44 (0) 754xx.
I can go strip out all the spaces and dashed and brackets etc to help normalise the lookup data but I am just wondering if I can do something about the source number.

Is there a call I can make to have a number converted to the international format?
I have seen the PhoneNumberUtils function formatNumber
http://developer.android.com/reference/android/telephony/PhoneNumberUtils.html
Will this help me convert the 0754xxx number to the +44754xx number?
I am not experienced enough to know how to use PhoneNumberUtils in Basic4Andriod so I can't try it.

Any suggestions are welcomed.

Thanks

dave
 

bsnqt

Active Member
Licensed User
@drenwick

Besides, you may want to know that in the Call Log, Missed call, SMS etc. database (provided by android Telephony provider) they have a field called "normalized" numbers, where all numbers are converted to International format...for example +6564000000. Take a look at ContactUtils library by Erel and search for my post please.

Good luck.
 

bsnqt

Active Member
Licensed User
You can use reflection library for that:
B4X:
Sub FormatNumber(n As String) As String
   Dim r As Reflector
   Return r.RunStaticMethod("android.telephony.PhoneNumberUtils", "formatNumber", _
     Array As Object(n), Array As String("java.lang.String"))
End Sub
Log(FormatNumber("972-52-2323232"))
Hi Erel, I tried the code you posted but for some reasons it does not work for me.

I put in the following string "091 2-217-25720" and it gives me exactly the same :( Why???
 

bsnqt

Active Member
Licensed User
Sorry for my stupid question again :) but I don't understand your answer.
Can you please give more explanation.
When I input "091 2-217-25720" I would expect it give me back the string like "091221725720" (without slash, space, dash, dot...)?
(I feel I am older and older :()
 

NJDude

Expert
Licensed User
That function will add dashes, for example, if you enter 2124446666 the output will be 212-444-6666, depending on where you are and the phone format for your area, the example I posted applies to USA numbers; if you want to remove any dashes then you will have to do it manually using REPLACE.
B4X:
...
 
Log(FormatNumber("2124446666").Replace("-", ""))
 
...
 

bsnqt

Active Member
Licensed User
Ok ok now it is clearer for me :) (as I said, getting older now haiz... :()

Just last question to make sure I correctly understand your point (so that other members can benefit too):

Scenario: Let say I (or the user) get an incoming call, the caller number is something like this (as it is a local number) "0122555777".
And in my Blacklist the stored number is using the format: "+65122555777" (with "+" and the country code, in International format)
I will need to use my stored number (String) to compare with caller's number (String) to see whether the caller is in Blacklist or not.

Question: The following will be true?

FormatNumber(incoming call's number) = FormatNumber(my stored number)

i.e FormatNumber("0122555777") = FormatNumber("+65122555777")
Appreciate your help NJDude :)

The reason I am asking so many questions is that I have done a call blocker, but when comparing the numbers I have to reformat them manually: I have to remove all dashes and spaces, only accept numeric and then get substring of them let say only last 7 characters...and lastly I compare those 7 characters against each other, so it is a very dirty coding. The issue will come as sometimes you have a call with only 3 or 4 characters (for example +1310 or +1010 etc.)

Regards,
 
Last edited:

NJDude

Expert
Licensed User
You will have to save the number exactly as needed, if you do this:
B4X:
FormatNumber("0122555777") = FormatNumber("+65122555777")
It will always be false.
 

bsnqt

Active Member
Licensed User
Ok understand now.

Perhaps, in the said scenario, I should use the following?
public static boolean compare (Context context, String a, String b)
Added in API level 5
Compare phone numbers a and b, and return true if they're identical enough for caller ID purposes. Checks a resource to determine whether to use a strict or loose comparison algorithm.

(from the link that drenwick has given above)
But if that is the case, then Erel's answer is not answering to question asked by drenwick :)? (Please correct if I am wrong again)
 
Top