3

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.

Ezio123
  • 376
  • 1
  • 4
  • 11

2 Answers2

8

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.

ChaturaM
  • 1,507
  • 18
  • 32
0

` Try this This is used to get the last mesage received from a address. if you want to read all the messages iterate this cursor.

 Uri mSmsinboxQueryUri = Uri.parse("content://sms/inbox");
 Cursor cursor1 = getContentResolver().query(mSmsinboxQueryUri,
            new String[] { "body", "address" }, null, null, null);

 if (cursor1.getCount() > 0) {
        cursor1.moveToFirst();
        String address = cursor1.getString(1);
        if (address.contains("Google")) {

            String body = cursor1.getString(0);

        }

    }

`