Search code examples
androidbroadcastreceiverandroid-broadcastreceiver

Displaying all SMS from one specific sender in my android app


My app is currently displaying all the SMS in the inbox with this code:

public void onClick(View v) {

    if (v == btnInbox) {

        // Create Inbox box URI
        Uri inboxURI = Uri.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.query(inboxURI, reqCols, null, null, null);

        // Attached Cursor with adapter and display in listview
        adapter = new SimpleCursorAdapter(this, R.layout.row, c,
                new String[]{"body", "address"}, new int[]{
                R.id.lblMsg, R.id.lblNumber});
        lvMsg.setAdapter(adapter);

    }
}

and I want to display only messages from one specific sender, what will i change to eliminate messages from the other sender?


Solution

  • apply selection arguments to filter the contents based on address field.

    Actual code becomes:

    String[] selectionArgs = { "12672631" }; //add the phone number here
    Cursor c = cr.query(inboxURI, reqCols, "address=?", selectionArgs, null);