Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read all SMS from a particular sender

Tags:

android

sms

How do I read all SMSes from a particular sender to me? E.g. I want to scan a) the body, and b) the date/time of all SMSes that came from 'TM-MYAMEX' to the phone.

Some websites seem to indicate this can be read from "content://sms/inbox". I couldn't figure out exactly how. Also not sure if it is supported on most phones. I am using a Galaxy S2.

like image 478
KalEl Avatar asked Jun 03 '12 12:06

KalEl


People also ask

How can I read SMS messages from the device programmatically in Android?

parse("content://sms/inbox"); // List required columns String[] reqCols = new String[]{"_id", "address", "body"}; // Get Content Resolver object, which will deal with Content Provider ContentResolver cr = getContentResolver(); // Fetch Inbox SMS Message from Built-in Content Provider Cursor c = cr.

How do I check SMS permission on Android?

To set the app's permission on a device or emulator instance, choose Settings > Apps > SMS Messaging > Permissions, and turn on the SMS permission for the app.

How can I read SMS from mobile?

Messages by Google enables you to view your Android text messages through any device which uses an internet browser, and even send text messages using that device! For this method, you'll need the "Messages" by Google app on your Android device.


3 Answers

try this way:

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='123456789'", 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" />
like image 174
ρяσѕρєя K Avatar answered Oct 19 '22 03:10

ρяσѕρєя K


try this, its a easy way for reading message from inbox.

public class ReadMsg extends AppCompatActivity {

ListView listView;
private static final int PERMISSION_REQUEST_READ_CONTACTS = 100;
ArrayList smsList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_read_msg);
    listView = (ListView)findViewById(R.id.idList);

    int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS);

    if (permissionCheck == PackageManager.PERMISSION_GRANTED){
        showContacts();
    }else{
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_SMS},PERMISSION_REQUEST_READ_CONTACTS);
    }
}

private void showContacts() {
    Uri inboxURI = Uri.parse("content://sms/inbox");
    smsList = new ArrayList();
    ContentResolver cr = getContentResolver();


    Cursor c = cr.query(inboxURI,null,null,null,null);
    while (c.moveToNext()){
        String Number = c.getString(c.getColumnIndexOrThrow("address")).toString();
        String Body= c.getString(c.getColumnIndexOrThrow("body")).toString();
        smsList.add("Number: "+ Number + "\n" + "Body: "+ Body );
    }
        c.close();
    ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, smsList);
    listView.setAdapter(adapter);
}

}

Add this permission into Manifests.

<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
like image 3
Tabish khan Avatar answered Oct 19 '22 03:10

Tabish khan


 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        inbox = (Button) findViewById(R.id.inbox);
        list = (ListView) findViewById(R.id.list);
        arlist = new ArrayList<String>();
        inbox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri inboxUri = Uri.parse("content://sms/inbox");
                String[] reqCols = {"_id", "body", "address"};
                ContentResolver cr = getContentResolver();
                Cursor cursor = cr.query(inboxUri, reqCols, "address='+919456'", null, null);
                adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.msg_content_layout, cursor,
                        new String[]{"body", "address"}, new int[]{R.id.txt1, R.id.txt2});
                list.setAdapter(adapter);
            }
        });

    }

Here I have added the number +919456 as the value of the address field.

Apart from this you need to add the READ_SMS permission to manifest:

like image 2
Anubhav Avatar answered Oct 19 '22 02:10

Anubhav