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
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.
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.
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.
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.
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.
onyl shows a list of emailadress and not my telephone list
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
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).