I've been trying to retrieve SMS information but have only been to retrieve everything BUT the display name of contacts. strDisplayName
only shows the numerical value of a particular contact and not the name itself. All other string version of data shows their corresponding information correctly (aside from "date"). I've tried other implementations of getting the display name from other answers, but doing so causes the app to crash when trying to test.
Uri smsData = Uri.parse("content://sms/" + folderName);
String[] id = new String[]
{
"_id", "person", "address",
"body", "date"
};
ContentResolver contentResolver = getContentResolver();
Cursor smsCursor = contentResolver.query(smsData, null, null, null, null);
// Retrieve index of data
int indexContactID = smsCursor.getColumnIndex(id[0]);
int indexDisplayName = smsCursor.getColumnIndex(id[1]);
int indexPhoneNumber = smsCursor.getColumnIndex(id[2]);
int indexMsg = smsCursor.getColumnIndex(id[3]);
int indexDate = smsCursor.getColumnIndex(id[4]);
if (indexMsg < 0 || !smsCursor.moveToFirst())
return;
mCustomAdapter.clear();
while (smsCursor.moveToNext())
{
// Retrieve string version of data
String strContactID = smsCursor.getString(indexContactID);
String strDisplayName = smsCursor.getString(indexDisplayName);
String strContactNumber = smsCursor.getString(indexPhoneNumber);
String strMsg = smsCursor.getString(indexMsg);
String strDate = smsCursor.getString(indexDate);
String[] textMessage = new String[]
{
strContactID, strDisplayName, strContactNumber,
strMsg, strDate
};
// Place collected data into custom adapter
mCustomAdapter.add(textMessage);
}
smsCursor.close();