i have been writing a program (see in my manual) that pulls the name and phone number from the contacts list and compares it to incoming sms numbers. However in testing i found a bug in my code that leaves me stumped.
If you have multiple numbers, such as work, home and mobile, it only chooses home and not the mobile number. So contact.phonenumber selects home, not mobile.
Of course only the mobile number is important and home won't send sms messages. How can i filter for only mobile numbers?
Here is my working code
I have been tooling around with the following but have failed to be able to integrate it into my code. Contact.PHONE_MOBILE always gives back 2 and not the number. I suspect if I could filter by Contact.PHONE_MOBILE it might only show contact.phonenumber as the mobile number but its not working.
attached is the full program
If you have multiple numbers, such as work, home and mobile, it only chooses home and not the mobile number. So contact.phonenumber selects home, not mobile.
Of course only the mobile number is important and home won't send sms messages. How can i filter for only mobile numbers?
Here is my working code
B4X:
Sub getcontacts
'create a Contacts to hold the contacts from your phone
Dim Contacts2 As Contacts2
'pass the contacts to a list to hold them. Note that each contact has about 6 or so fields, that at the moment you can't get to
listOfContacts = Contacts2.GetAll(True,False)
'loop through each of the contacts in your new list
For i = 0 To listOfContacts.Size - 1
'for each contact pass it to the Contact object. This object allows you to extract out each field held in field, so you can use Contact.phonenumber and Contact.DisplayName as well as many others.
Dim Contact As Contact
Contact = listOfContacts.Get(i)
'Logging to check how it works
Log(Contact) 'will print the fields to the LogCat
'We don't want contacts who don't have a phone number or contacts with short number such as your phone provider. A 3 digit number will screw with the next part fo the code where we cut the beginning off.
If Contact.phonenumber <>"" AND Contact.phonenumber.Length > 7 Then
'show the name and number from the constacts list in your listview and format it up to make it easy to read
lvphone.SingleLineLayout.Label.TextSize = 15
lvphone.SingleLineLayout.Label.TextColor = Colors.Black
lvphone.SingleLineLayout.ItemHeight = 40
lvphone.ScrollingBackgroundColor =Colors.Transparent
'data goes in here the name and number seperated by a :, this is what we use to get the name out later.
lvphone.AddSingleLine(Contact.DisplayName &": " & Contact.phonenumber)
End If
Next
I have been tooling around with the following but have failed to be able to integrate it into my code. Contact.PHONE_MOBILE always gives back 2 and not the number. I suspect if I could filter by Contact.PHONE_MOBILE it might only show contact.phonenumber as the mobile number but its not working.
B4X:
Dim c As Contact
Dim phones As Map
phones = Contact.GetPhones
Dim p As String 'Holds the result
p = "N/A"
For i = 0 To phones.Size - 1
p = phones.GetKeyAt(i)
If phones.GetValueAt(i) = Contact.PHONE_MOBILE Then Exit
Next
attached is the full program
Attachments
Last edited: