2016-10-10 2 views
-1

ich mitAndroid: Wie Kontakt Namen und die Nummer von recipient_ids

Uri uriSms = Uri.parse("content://mms-sms/conversations?simple=true"); 
    Cursor cursor = getContentResolver().query(uriSms, null,null,null,null); 
    cursor.moveToLast(); 
    while (cursor.moveToPrevious()) 
    { 
     String recipient_ids= cursor.getString(cursor.getColumnIndex("recipient_ids")); 
     String body = cursor.getString(cursor.getColumnIndex("snippet")); 

    } 

eine Liste von sms Gespräch kommen bin zu finden. Die "recipient_ids" gibt einen Wert wie 302, 301 259 usw. Was ich will ist eine Funktion, wo ich die "recipient_ids" als Parameter übergeben und es wird der Anzeigename des Kontakts (Wenn vorhanden, sonst die Nummer)

Antwort

0

Bitte überprüfen Sie diesen Code

private void fetchInbox() { 
    Uri inboxURI = Uri.parse("content://sms/inbox"); 
    String[] reqCols = new String[]{"_id", "address", "body"}; 
    ContentResolver cr = getContentResolver(); 
    Cursor c = cr.query(inboxURI, reqCols, null, null, null); 

    adapter = new SimpleCursorAdapter(this, R.layout.row, c, 
      new String[]{"body", "address"}, new int[]{ 
      R.id.lblMsg, R.id.lblNumber}, 0); 
} 
0

Diese Methoden Ihnen helfen, zu erreichen, was Sie wollen: getContactByRecipientId - Kontaktnummer von recipientId zu bekommen. getContactbyPhoneNumber - um den Anzeigenamen nach Telefonnummer zu erhalten.

public String getContactByRecipientId(long recipientId) { 

    String contact = ""; 
    Uri uri = ContentUris.withAppendedId(Uri.parse("content://mms-sms/canonical-address"), recipientId); 
    Cursor cursor = getContentResolver().query(uri, null, null, null, null); 

    try { 
    if (cursor.moveToFirst()) { 
     contact = getContactbyPhoneNumber(cursor.getString(0)); 
    } 
    } finally { 
    cursor.close(); 
    } 

    return contact; 
} 


public String getContactbyPhoneNumber(String phoneNumber) { 

    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); 
    String[] projection = {ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup.NORMALIZED_NUMBER }; 
    Cursor cursor = getContentResolver().query(uri, projection, null, null, null); 

    String name = null; 
    String nPhoneNumber = phoneNumber; 

    try { 

    if (cursor.moveToFirst()) { 
     nPhoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.NORMALIZED_NUMBER)); 
     name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME)); 
    } 

    } finally { 
    cursor.close(); 
    } 

    if(name != null){ // if there is a display name, then return that 
     return name; 
    }else{ 
     return nPhoneNumber; // if there is not a display name, then return just phone number 
    } 
} 
Verwandte Themen