Get name by phone nr.

XverhelstX

Well-Known Member
Licensed User
Longtime User
Heey,

I would like to know the name of the one who sended a message.

B4X:
Sub SI_MessageReceived (From As String, Body As String)
   MessageFrom1 = From
   MessageBody1 = Body
   
   
   Dim Contacts1 As Contacts
   Dim Contact1 As Contact
   Dim listOfContacts As List
   Dim list1 As List
   listOfContacts = Contacts1.GetAll
   For i = 0 To listOfContacts.Size - 1
       
       Contact1 = listOfContacts.Get(i)
      If Contact1.PhoneNumber = MessageFrom1 Then
         MessageFromName = Contact1.DisplayName
         StartActivity(AMessage)
      Else
         MessageFromName = MessageFrom1
         StartActivity(AMessage)
      End If
   
   Next 
End Sub

This seems to work, but I always got my phone number instead than the name. I know it's related to this (If Contact1.PhoneNumber = MessageFrom1 Then), but I want something like if string.contains (phonenumber) = ... etc

How would I solve this?

XverhelstX
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Yes. You can use a boolean variable named 'found'.
Set it to false before the For loop. If the contact was found set it to true.

After the loop check if found = false and start the activity.

Actually you can just move the StartActivity call out of the For loop. StartActivity doesn't happen immediately anyway. It happens once your current code finishes executing.
 
Upvote 0

XverhelstX

Well-Known Member
Licensed User
Longtime User
nope, still doesn't work..

What I think is that Contact1 is a full list of all my phone numbers. (Contact1 = listOfContacts.Get(i))
How is it possible than that Contact1.PhoneNumber is equal than as MessageFrom1 ?



B4X:
Sub SI_MessageReceived (From As String, Body As String)
   MessageFrom1 = From
   MessageBody1 = Body
   
   
   Dim Contacts1 As Contacts
   Dim Contact1 As Contact
   Dim blnFound As Boolean
   blnFound = False
   Dim listOfContacts As List
   Dim list1 As List
   listOfContacts = Contacts1.GetAll
   For i = 0 To listOfContacts.Size - 1
       
       Contact1 = listOfContacts.Get(i)
      If Contact1.PhoneNumber = MessageFrom1 Then
         MessageFromName = Contact1.DisplayName
         StartActivity(AMessage)
         blnFound = True
      End If
   
   Next 
   
   If blnFound = False Then
   MessageFromName = MessageFrom1
   StartActivity(AMessage)
   
   End If 
End Sub

Also note that the message displayed is +3247...... (+32 for belgium)
and that this is also the same number in my settings.

xverhelstx
 
Upvote 0

galimpic

Member
Licensed User
Longtime User
You should exit the loop after you find the number. I think that this forum should not be about general programming techniques, but about b4a specifics, and you should first work with hardcoded strings and make sure your program flow is OK before you go any further.
 
Upvote 0

XverhelstX

Well-Known Member
Licensed User
Longtime User
ok, I did this:

Dim Contacts1 As Contacts
Dim Contact1 As Contact
Dim blnFound As Boolean
blnFound = False
Dim listOfContacts As List
Dim list1 As List
listOfContacts = Contacts1.GetAll
Msgbox(listOfContacts,"")

in Resume, and in the messageboxs, it only shows email adress and not my telephone list.

why doesn't this show the telephone contact list?

XverhelstX
 
Last edited:
Upvote 0

XverhelstX

Well-Known Member
Licensed User
Longtime User
ok, the problem is that

B4X:
listOfContacts = Contacts1.GetAll

onyl shows a list of emailadress and not my telephone list :confused:

code:

B4X:
Sub Activity_Resume

   Dim Contacts1 As Contacts
   Dim myContact As Contact
   Dim blnFound As Boolean
   blnFound = False
   Dim listOfContacts As List
   Dim list1 As List
   listOfContacts = Contacts1.GetAll
   
   For i = 0 To listOfContacts.Size - 1
      myContact = listOfContacts.Get(i)
      Dim mob As Map
      Dim mobile As String
      mob = myContact.GetPhones
         For j = 0 To mob.Size - 1

            If mob.GetValueAt(j) = "+32479745494" Then
               mobile = mob.GetKeyAt(j)
               MessageFromName = mobile
               'StartActivity(AMessage)
               blnFound = True
               Msgbox(MessageFromName,"")
               Exit
            End If
         Next
   
   Next 
   Msgbox(listOfContacts,"")
End Sub
 
Last edited:
Upvote 0

galimpic

Member
Licensed User
Longtime User
That looks more like it... it would be nice if someone who is making an app using phonebook, while he's at it creates a library with some of these procedures (e.g. returning a contact based on a phone number).
 
Upvote 0
Top