Multiple answers are already available but i think all of them are missing an important part of this question.
Before reading data from an internal database or its table we have to understand how data is stored in it and only then we can find the solution to the above question that is :
How can I read SMS messages from the device programmatically in Android?
In android SMS table looks like this
Now you can select whatever you want from the database. In our case we only need
id,address and body
In case of reading SMS:
1.Ask for permissions
int REQUEST_PHONE_CALL = 1;
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_SMS}, REQUEST_PHONE_CALL);
}
or
<uses-permission android:name="android.permission.READ_SMS" />
2.Now your code goes like this
// 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.a1_row, c,
new String[]{"body", "address"}, new int[]{
R.id.A1_txt_Msg, R.id.A1_txt_Number});
lst.setAdapter(adapter);
I hope this will be helpful.
Thanks.