Domanda

The code snippet below returns basic SMS conversation data:

Cursor cursor = activity.getContentResolver().query(Uri.parse( "content://mms-sms/conversations?simple=true"), null, null, null, "normalized_date desc" );
if(cursor.moveToFirst())
  String recipient_ids = cursor.getString(3);

My question is that how can I get a phone's contact data given that recipient_ids? In this case I need to retrieve the contact number and contact display_name.

Your help is greatly appreciated. Thanks in advance!

È stato utile?

Soluzione

Try this:

public String getContactData(String id){
  String number;

  Cursor phones = fa.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id, null, null); 
    if(phones.moveToFirst()) { 
      number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));       
    } 

  phones.close();
  return number;
}

Hope it helps!

Altri suggerimenti

This is, what's working in my app:

private String getContactNumber(final long recipientId) {
    String number = null;
    Cursor c = getContentResolver().query(ContentUris
            .withAppendedId(Uri.parse("content://mms-sms/canonical-address"), recipientId),
            null, null, null, null);
    if (c.moveToFirst()) {
        number = c.getString(0);
    }
    c.close();
    return number;
}

The contact numbers are saved in some table called canonical-address.
It was kind of buggy a few years ago. Updates on the contact did not propagate through this table properly. But I think that's fine now.

You basically need to parse the (list of) ids into single ids. Then query the database for each of them.
You could use one single query for all ids together, though.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top