Ezio123 Ezio123 - 1 month ago 24
Android Question

Android query SMS inbox by sender name

I want to get the list of SMS in the phone by a specific sender. These SMS are sent from a SMS gateway so I don't have the sender's number. However, I know the sender's name such as "Google".

This is what I have tried so far

final String SMS_URI_INBOX = "content://sms/inbox";
Uri uri = Uri.parse(SMS_URI_INBOX);
String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };
Cursor cur = getContentResolver().query(uri, projection, "person LIKE'%" + "Google" + "'", null, "date asc");


However, the cursor is empty.

Answer

You are almost there

final String SMS_URI_INBOX = "content://sms/inbox";
Uri uri = Uri.parse(SMS_URI_INBOX);
String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };
Cursor cur = getContentResolver().query(uri, projection, "address LIKE '%Google%'", null, "date asc");

Instead of querying for "person", query for "address". SMS gateways use name as the address.