2012-03-25 6 views
0

Ich habe den folgenden Code, um Details eines Kontakts zu erhalten. "Daten": ist der URI, den ich nach Auswahl des Kontaktes zurückbekomme.LOOKUP_KEY und CONTENT_LOOKUP_URI

Ich muss sicher sein, dass ich in Zukunft den richtigen Kontakt bekomme, also was sollte ich für zukünftige Verwendung sparen? Ist es "LookupUri" oder "LookupKey"?

Cursor c = activity.managedQuery(data, null, null, null, null); 
    c.moveToFirst(); 
    String lookupKey = c.getString(c.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY)); 
    c.close(); 

    // Next use that key to access the details of the contact in order to get the name and the photo 
    // Also, save it for future use. 
    // It will be used when we fetch the details from the database since the photo itself is not saved. 
    Uri lookupUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI,lookupKey); 

    Uri uri = ContactsContract.Contacts.lookupContact(activity.getContentResolver(), deviceDetails.lookupUri); 

Antwort

2

LookupKey ist der eindeutige Bezeichner, den Sie speichern möchten.

FYI; Es gibt einen Fehler in 2.1, bei dem sich nicht synchronisierte Kontakte LookupKey ändert, wenn der Name geändert wird.

0

Das funktioniert. Mein Beispiel sucht nach dem Namen. Fügen Sie die gewünschten Felder in der Projektion hinzu oder entfernen Sie sie.

private String getContactNameFromAndroidKey (String key) 
{ 
    // Run query 
    Uri uri = Uri.parse (ContactsContract.Contacts.CONTENT_LOOKUP_URI + "/" + key); 

    String[] projection = new String[] { 
    Contacts._ID, 
    Contacts.DISPLAY_NAME, 
    }; 

    Cursor cursor = context.getContentResolver().query (
    uri, 
    projection, 
    null, 
    null, 
    null); 

    if (!cursor.moveToNext()) // move to first (and only) row. 
    throw new IllegalStateException ("contact no longer exists for key"); 
    String name = cursor.getString(1); 
    cursor.close(); 

    return name; 
} 
Verwandte Themen