Android SMS Content (content://sms/sent)
Asked Answered
H

1

20

I'm having a problem reading the SMS messages from the device. When acquiring a content provider for the URI content://sms/inbox, everything is fine. I can read the person column to find the foreign key into the people table and ultimately reach the contact and their name.

However, I also want to traverse the sent messages too. When reading from content://sms/sent, the person field always appears to be 0.

Is this the correct field to be reading to locate the recipient data for the sent message? If so - any idea why mine is always 0?

All my testing has been done in the emulator and I've created 3 contacts. I've sent messages to those contacts from the emulator in the normal manner you'd send a message.

Just to reiterate, I can see the 4 sent messages and read the associated body text. My problem is that I can't seem to read the "person" ID and hence I can't work out who the recipient is.

Hulen answered 19/3, 2009 at 14:28 Comment(0)
H
18

Use the address column. I guess the person column is ignored because people can send SMSs to phone numbers that are not in the contacts list.

// address contains the phone number
Uri phoneUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, address);
if (phoneUri != null) {
  Cursor phoneCursor = getContentResolver().query(phoneUri, new String[] {Phones._ID, Contacts.Phones.PERSON_ID}, null, null, null);
  if (phoneCursor.moveToFirst()) {
    long person = phonesCursor.getLong(1); // this is the person ID you need
  }
}
Honky answered 19/3, 2009 at 17:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.