Android Question National and international phone numbers

Troberg

Well-Known Member
Licensed User
Longtime User
I've made an app that intercepts SMS messages (using this method: https://www.b4x.com/android/forum/t...cepting-sms-messages-in-the-background.20103/). It then needs to map them to the contacts list, to get a person.

However, a problem occurred. The contacts list usually only has national numbers (for example, in Sweden, 0123-456789), while the number I get in the SMS message is the international number (in the preceding example, +46123456789).

Now, I can do some things easily to make the comparison easier, such as stripping the string down to only numbers, but that still doesn't fix the root problem here. Is there any API or library that can go from international numbers to international?

Sure, I could whip out some code to do it, but that would require me to do a lot of research on different national phone number schemes for all countries that might run my app. It would also require me to keep up to date if numbering schemes change. That's a lot of work, so I'd prefer not to.

Any hints?
 

Troberg

Well-Known Member
Licensed User
Longtime User
Well, I got a fully qualified international number (for example +46123456789), so I could just check for the country code.
 
Upvote 0

Federico956

Member
Licensed User
Longtime User
I thinked about a database with all the international prefixes. Example: Italy +39 12345678, you check the match (+39 is in your db) and you delete the prefix (you have 12345678). Have I understand your problem?
 
Upvote 0

Troberg

Well-Known Member
Licensed User
Longtime User
Well, that's one way of doing it, but that would mean that I would have to gather information on every number format and how to handle it, and keep that information constantly up to date. I'm looking for something a bit simpler...
 
Upvote 0

Troberg

Well-Known Member
Licensed User
Longtime User
As a quick and dirty solution, I've done the following (and will probably improve it later):

B4X:
'No2 is expected to be international
'Both numbers are expected to be stripped down to numbers and +
Sub ComparePhoneNo(No1 As String, No2 As String) As Boolean
    Dim res As Boolean = False
    If No1.StartsWith("+") Then 'International number
        If No1=No2 Then
            res = True
        End If
    Else 'National number
        Dim TNo1 As String = No1
        If No1.StartsWith("0") Then
            TNo1 = TNo1.SubString(1)
        End If
        If No2.EndsWith(TNo1) Then
            res=True
        End If
    End If
    Return res
End Sub

Basically, if the contacts number is international, it's just a plain comparison. If not, I take the contacts number, trim the first digit if it is a zero, then checks if the international number ends with it.

It should work with most numbering systems. If anyone know about a system where it won't work, please tell me.
 
Upvote 0
Top