Using SL4A and Python, is there an easy way to get a Contact ID from a phone number?
The phone number is from the 'address' field of an SMS.
I am trying to avoid searching through all contacts.
I use m = droid.smsGetMessageById(id, None).result
to get the SMS message. The result looks like:
{u'read': u'1', u'body': u"Hello! Your mobile bill's now ready to view at virginmobile.co.uk/youraccount. We'll collect your Direct Debit of 12.12 on or just after 19th Nov.", u'_id': u'1323', u'date': u'1415372649502', u'address': u'1234567890'}
The value in the address
entry is the phone number that sent the SMS.
I want to get a contact ID using this number, but, if possible, I want to avoid searching all my contacts.
I figured it out:
def contactFromPhone(phone):
uri='content://com.android.contacts/data'
filter='data4 LIKE ?'
args=['%'+phone+'%']
columns=['contact_id']
contacts=droid.queryContent(uri, columns, filter, args).result
cs=[]
if len(contacts)>0:
for c in contacts:
cs.append(c['contact_id'])
return cs
'phone' is a normalized phone number
returns a list of contact_id