There are many ways to accomplish this, but the best way would be to find the thread-id of the conversation thread with that contact/number, and then query the thread-id for all messages.
Note that there are a lot of issues with different API levels, devices with dual-sim, mms/sms messages, etc.
Query for all threads (with their thread-id):
// This projection is suitable for API-19 and above only
String[] projection = { Threads._ID, Threads.RECIPIENT_IDS, "sort_index", Threads.SNIPPET }
Uri uri = Threads.CONTENT_URI.buildUpon().appendQueryParameter("simple", "true").build();
Cursor cur = getContentResolver().query(uri, projection, selection, null, "sort_index" + " DESC");
To translate between a RecipientId and a phone number, you can copy Android's RecipientIdCache class to your project. It's a singleton class used for converting id to number.
Query for all messages by thread-id:
// This projection is suitable for API-19 and above only
String[] projection = new String[]{
MmsSms.TYPE_DISCRIMINATOR_COLUMN,
Telephony.Sms._ID,
Telephony.Sms.BODY,
"sort_index",
Telephony.Sms.DATE_SENT
};
Uri uri = ContentUris.withAppendedId(Threads.CONTENT_URI, id);
Cursor cur = getContentResolver().query(uri, projection, null, null, null);