0

I've been trying to retrieve SMS information but have only been to retrieve everything BUT the display name of contacts. strDisplayNameonly 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();

1 Answers1

0

Who told you "person" is the contact's display-name?

According to the docs:

PERSON

The ID of the sender of the conversation, if present.

This is actually a "sender-id" this is never a contact-id, as an sms can be sent from a contact, a non-contact phone number, a non-phone-number (like GOOGLE or FACEBOOK) and from a group containing two or more of the above types.

If you'd like to get check if this is a contact, and if it is, get the contact's name, you need to pass two steps:

  1. Use the canonical-address table to translate between id and phone-number (address). You can copy and use Android's helper class for this RecipientIdCache
  2. In case the address is a single phone number, use the PHONE_LOOKUP table to look for a contact with that number, this will help: https://stackoverflow.com/a/7967182/819355
marmor
  • 27,641
  • 11
  • 107
  • 150
  • I tried no. 2 using the string of the address (even removing '+' from the number), but it the app still crashes. –  Jul 25 '17 at 05:04
  • what's the stack trace? what does it complain about? – marmor Jul 25 '17 at 05:37
  • also, print the address you get to log, to make sure you're using a valid input – marmor Jul 25 '17 at 05:38
  • It complains about `Cursor contactLookup` in `getContactDisplayNameByNumber` in my MainActivity. –  Jul 25 '17 at 06:27
  • can you post the full stack trace on www.pastebin.com ? – marmor Jul 25 '17 at 06:29
  • you don't have permission to read contacts... read this: https://developer.android.com/training/permissions/requesting.html and this: https://stackoverflow.com/questions/35163953/android-runtime-permissions-how-to-implement – marmor Jul 25 '17 at 06:37
  • I've tried doing what it says from what I understand, but my MainActivity doesn't like more than one Cursor object created. Plus I already had permission to read contacts in my manifest –  Jul 25 '17 at 08:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/150061/discussion-between-marmor-and-spicyweenie). – marmor Jul 25 '17 at 08:31