0

I want to display in listview all sent sms messages from my device, currently I only managed to display all received messages in listview. Do you have any solution on how to do it? below is my current code, Thanks in advance to your response.

 ArrayList<String> items = new ArrayList<String>();
 ArrayAdapter arrayAdapter;

 public void displaymessage(){
    arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, items);
    ContentResolver cResolver = getContentResolver();
    Cursor smsInboxCursor = cResolver.query(Uri.parse("content://sms/inbox"),null,null,null,"date");
    int indexBody = smsInboxCursor.getColumnIndex("body");
    if (indexBody < 0 || !smsInboxCursor.moveToFirst()) return;
    do{
       String str = smsInboxCursor.getString(indexBody) ;
       arrayAdapter.add(str);
    }while (smsInboxCursor.moveToNext());
}
MackyRV
  • 15
  • 8
  • If you mean that you want just the sent messages, change `"content://sms/inbox"` to `"content://sms/sent"`. If you want everything that you're allowed to read from the Provider – which is only the inbox and sent messages for non-default apps, since Marshmallow – you can use `"content://sms"`. – Mike M. Jun 03 '20 at 08:06
  • 1
    I forgot to mention, there are several SMS-related constants available in the SDK that you could use to replace most of those magic `String` literals. They're under [`android.provider.Telephony.Sms`](https://developer.android.com/reference/android/provider/Telephony.Sms). For example, you could replace `Uri.parse("content://sms/inbox")` with `Telephony.Sms.Inbox.CONTENT_URI`, and `Uri.parse("content://sms/sent")` with `Telephony.Sms.Sent.CONTENT_URI`, etc. The column names are available, too; e.g., `"address"` -> `Telephony.Sms.ADDRESS`, `"body"` -> `Telephony.Sms.BODY`, etc. Just FYI. Cheers! – Mike M. Jun 03 '20 at 10:12

0 Answers0