SMS message for SMS messages by sender name

I want to receive a list of SMS in the phone by a specific sender. These SMS are sent from the SMS gateway, so I don't have the sender number. However, I know the sender's name, for example "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.

+3


source to share


3 answers


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 asking for "person", asking for "address". SMS gateways use the name as the address.

+6


source


Try to do this -

StringBuilder smsBuilder = new StringBuilder();
       final String SMS_URI_INBOX = "content://sms/inbox"; 
        final String SMS_URI_ALL = "content://sms/";  
        try {  
            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 desc");
             if (cur.moveToFirst()) {  
                int index_Address = cur.getColumnIndex("address");  
                int index_Person = cur.getColumnIndex("person");  
                int index_Body = cur.getColumnIndex("body");  
                int index_Date = cur.getColumnIndex("date");  
                int index_Type = cur.getColumnIndex("type");         
                do {  
                    String strAddress = cur.getString(index_Address);  
                    int intPerson = cur.getInt(index_Person);  
                    String strbody = cur.getString(index_Body);  
                    long longDate = cur.getLong(index_Date);  
                    int int_Type = cur.getInt(index_Type);  

                    smsBuilder.append("[ ");  
                    smsBuilder.append(strAddress + ", ");  
                    smsBuilder.append(intPerson + ", ");  
                    smsBuilder.append(strbody + ", ");  
                    smsBuilder.append(longDate + ", ");  
                    smsBuilder.append(int_Type);  
                    smsBuilder.append(" ]\n\n");  
                } while (cur.moveToNext());  

                if (!cur.isClosed()) {  
                    cur.close();  
                    cur = null;  
                }  
            } else {  
                smsBuilder.append("no result!");  
            } // end if  
            }
        } catch (SQLiteException ex) {  
            Log.d("SQLiteException", ex.getMessage());  
        }  

      



AndroidManifest.xml:

<uses-permission android:name="android.permission.READ_SMS" />

      

+3


source


`Try this Used to get the last message received from the address. if you want to read all messages, iterate over that 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);

        }

    }

      

`

0


source







All Articles