22

Ich brauche alle Telefonkontakte und ihre E-Mail erhalten Adresse und Foto uri:Android Kontakte Anbieter erhalten nur die Telefonkontakte mit allen E-Mail

Dies ist, was tue:

private void getContacts() { 

     ContentResolver cr = getContentResolver(); 
     Cursor cur = cr.query(Contacts.CONTENT_URI, null, null, null, Contacts.DISPLAY_NAME); 

     if (cur.getCount() > 0) { 
      while (cur.moveToNext()) { 

       // if 
       // (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) 
       // > 0) { 

       Contact contact = new Contact(); 

       String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); 

       Uri uri = getContactPhotoUri(Long.parseLong(id)); 
       // set photoUri 
       contact.setContactPhotoUri(uri); 

       // set name 
       contact.setContactName(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))); 

       // get the phone number 
       Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null); 
       while (pCur.moveToNext()) { 

        // set phone munber 
        contact.setContactNumber(pCur.getString(pCur 
          .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))); 
        contacts.add(contact); 

       } 
       pCur.close(); 

       // get email and type 
       Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, 
         ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[] { id }, null); 
       while (emailCur.moveToNext()) { 
        // This would allow you get several email addresses 
        // if the email addresses were stored in an array 

        // set email 
        contact.setContactEmail(emailCur.getString(emailCur 
          .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA))); 

        contacts.add(contact); 

       } 
       emailCur.close(); 

      } 
     } 

     cur.close(); 
     contactAdapter = new ContactAdapter(this, R.id.contactList, contacts); 

     // } 

    } 

    public Uri getContactPhotoUri(long contactId) { 
     Uri photoUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId); 
     photoUri = Uri.withAppendedPath(photoUri, Contacts.Photo.CONTENT_DIRECTORY); 
     return photoUri; 
    } 

Mein Problem ist immer all Kontakte einschließlich Google Mail-Kontakte, ich möchte nicht, dass Google Mail-Kontakte enthalten sind. Und die Zeit ist auch sehr langsam. Wie optimiere ich das, ich weiß, es dauert Zeit coz ich benutze viele Cursors .. aber nicht wissen, wie man einen einzigen cusror, der mir Name E-Mail-Nummer Foto uri geben kann ... Danke!

FINAL AKTUALISIERT:

private void getContacts() { 

    ContentResolver cr = getContentResolver(); 

    Cursor cur = cr.query(Data.CONTENT_URI, new String[] { Data.CONTACT_ID, Data.MIMETYPE, Email.ADDRESS, 
      Contacts.DISPLAY_NAME, Phone.NUMBER }, null, null, Contacts.DISPLAY_NAME); 

    Contact contact; 

    if (cur.getCount() > 0) { 

     while (cur.moveToNext()) { 

      String id = cur.getString(cur.getColumnIndex(Data.CONTACT_ID)); 

      String mimeType = cur.getString(cur.getColumnIndex(Data.MIMETYPE)); 

      if (allContacts.containsKey(id)) { 
       // update contact 
       contact = allContacts.get(id); 
      } else { 
       contact = new Contact(); 
       allContacts.put(id, contact); 
       // set photoUri 
       contact.setContactPhotoUri(getContactPhotoUri(Long.parseLong(id))); 
      } 

      if (mimeType.equals(StructuredName.CONTENT_ITEM_TYPE)) 
       // set name 
       contact.setContactName(cur.getString(cur.getColumnIndex(Contacts.DISPLAY_NAME))); 

      if (mimeType.equals(Phone.CONTENT_ITEM_TYPE)) 
       // set phone munber 
       contact.setContactNumber(cur.getString(cur.getColumnIndex(Phone.NUMBER))); 

      if (mimeType.equals(Email.CONTENT_ITEM_TYPE)) 
       // set email 
       contact.setContactEmail(cur.getString(cur.getColumnIndex(Email.ADDRESS))); 

     } 
    } 

    cur.close(); 
    // get contacts from hashmap 
    contacts.clear(); 
    contacts.addAll(allContacts.values()); 

    // remove null contacts 
    for (Contact _contact : contacts) { 

     if (_contact.getContactName() == null && _contact.getContactNumber() == null 
       && _contact.getContactEmail() == null) { 
      contacts.remove(_contact); 
      break; 
     } 

    } 

    contactAdapter = new ContactAdapter(this, R.id.contactList, contacts); 
    contactAdapter.notifyDataSetChanged(); 

} 

public Uri getContactPhotoUri(long contactId) { 
    Uri photoUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId); 
    photoUri = Uri.withAppendedPath(photoUri, Contacts.Photo.CONTENT_DIRECTORY); 
    return photoUri; 
} 

Antwort

22

Sie sollten in einer Abfrage auf Data.CONTENT_URI, benötigt alle Informationen erhalten können, Check out „android.provider.ContactsContract.Data“ Tabelle und die Beispiele auf, wie verschiedene Arten von Daten per E-Mail, Telefon, Foto usw. ... http://developer.android.com/reference/android/provider/ContactsContract.Data.html

zum Beispiel zur Abfrage:

Cursor data = cntx.getContentResolver().query(Data.CONTENT_URI, new String[] {Data._ID,Data.MIMETYPE,Email.ADDRESS,Photo.PHOTO},Data.CONTACT_ID + "=?" + " AND " + "(" + Data.MIMETYPE + "='" + Photo.CONTENT_ITEM_TYPE + "' OR " + Data.MIMETYPE + "='" + Email.CONTENT_ITEM_TYPE +"')", 
         new String[] {String.valueOf(contactId)}, null); 

Wenn Sie alle Informationen zu einer bestimmten contactId benötigen, können Sie theoretisch nach allen Kontakten fragen und die Informationen selbst sortieren.

Als gmail Kontakte zum Filtern diesen ein komplexeres Problem ist, werfen Sie einen Blick auf ACCOUNT_NAME/TYPE http://developer.android.com/reference/android/provider/ContactsContract.RawContacts.html Parameter und eine Diskussion zu diesem Thema hier: What is the default Account Type/Name for contacts on Android Contact Application?

+0

wie etwa Anzeigenamen von oben Abfrage bekommen .. – sukarno

+0

Vergessen Sie das Filtern nach Gmail-Konto .. müssen alle Kontakte E-Mail-Nummer Anzeige Namen und Foto Uri in einer benutzerdefinierten Abfrage .. um die Leistung zu verbessern – sukarno

+0

In der ersten Verbindung, die ich zur Verfügung gestellt, können Sie sehen, dass die Datentabelle auch mit ContactContarct zusammengeführt wird .Kontakte -> so können Sie nach Contacts.DISPLAY_NAME fragen. Berücksichtigen Sie, dass die Datentabelle viele Datensätze enthält, so dass Sie sicherstellen müssen, dass Sie den "richtigen" betrachten. – Raanan

Verwandte Themen