Android Question [SOLVED] How to match and retrieve SMS message sender name from saved contacts

masterleous

Member
Licensed User
Longtime User
Hi all,

I need some help in getting contact name of person from SMS Message. I am unable to get contact from SMS Message. Lets for example we have following contacts in our saved phone book, few with “+” plus sign and country code, few without. In following case +1 is country code;

Person1 = +1-111-222-3333
Person2 = +1-222-333-4444
Person3 = 333-222-1111
Person4= 444-333-2222

Most of the SMS Center send message with country code and plus sign like +1-111-111-1111, For those saved contact who are saved with plus sign and country code will easily to catch using “sms.PersonID” but “sms.PersonID” results “null” for those contact which are not properly saved with plus sign and country code. I also tried to incorporate “ContactUtili…” class by “Erel” but failed to get names of sender.

What will be the correct method to perform this action precisely?

Thanks
 

masterleous

Member
Licensed User
Longtime User
Have you tried ContactUtils.FindContactsByPhone?
I tried with that before with following code, but result was not obtained.
B4X:
Dim cu As ContactsUtils
cu.Initialize

Dim list1As List
list1=cu.FindContactsByPhone(Sms1.Address,False,False)

Dim cuContactSearch As cuContact
Dim ContactSearch As Contact

cuContactSearch = list1.get(0)      '<--- This one is giving Error
ContactSearch = list1.get(0)        '<--- This one is giving Error too

Log(cuContactSearch)
Log(ContactSearch )

Following error occurred
B4X:
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

What will be the correct process to do this act?

Thanks
 
Upvote 0

masterleous

Member
Licensed User
Longtime User
You should check the list size before you try to get the first element.
I tried with that, list1.size always giving "ZERO" value for those contacts, which are installed without plus sign and country code.

Isn't any other way to do this job? someway to bypass country code and than check rest of contact number.
 
Upvote 0
Top