2016-10-29 9 views
0

Ich weiß, um die Namen der Kontakte, die Code unten ist, abrufen. Aber was soll ich in diesem Code ändern, um auch die Telefonnummern der Kontaktliste zu haben?Xamarin android Lesen von Kontakten und Nummern

var uri = ContactsContract.Contacts.ContentUri; 

      string[] projection = { ContactsContract.Contacts.InterfaceConsts.Id, 
    ContactsContract.Contacts.InterfaceConsts.DisplayName }; 

      var cursor = ManagedQuery(uri, projection, null, null, null); 

      var contactList = new List<string>(); 

      if (cursor.MoveToFirst()) 
      { 
       do 
       { 
        String phoneNumber = cursor.GetString(cursor.GetColumnIndex(ContactsContract.CommonDataKinds.Phone.Number)); 

       contactList.Add(cursor.GetString(
         cursor.GetColumnIndex(projection[1]))); 
      } while (cursor.MoveToNext()); 
     }     

Antwort

0

Ich stieß auf Ihre Frage, während ich vor kurzem einen ähnlichen Code schrieb. Das einzige, was Sie benötigen, um Telefonnummern abzurufen, ist:

  1. eine weitere Abfrage ContactsContract.CommonDataKinds.Phone.ContentUri zu Uri Stellen;
  2. Projektion festlegen, um ContactsContract.CommonDataKinds.Phone.Number einzuschließen; und
  3. basierend Wählen Sie auf id, dh Auswahlparameter sollte enthalten „_id =“ + ContactID

Ihre Abfrage sollte wie folgt aussehen:

string[] projection = { ontactsContract.CommonDataKinds.Phone.Number }; 
string selection = "_id = " + contactId; 

var cursor = ContentResolver.Query(ContactsContract.CommonDataKinds.Phone.ContentUri, projection, selection, null, null); 

Diese Abfrage wird bringen Sie Telefonnummern für eine Kontakt-ID, aber im Code unten, wegen der Leistungsprobleme, habe ich nur einen Anruf für alle Telefonnummern und dann vergeben jeden Vertrag ihre Nummern. Ich hoffe es hilft.

private List<Contact> GetContactList() 
{ 
    List<Contact> contacts = new List<Contact>(); 
    string[] projection = { 
       ContactsContract.Contacts.InterfaceConsts.Id, 
       ContactsContract.Contacts.InterfaceConsts.DisplayName, 
       ContactsContract.Contacts.InterfaceConsts.PhotoUri 
      }; 
    var uri = ContactsContract.Contacts.ContentUri; 
    ICursor cursor = ContentResolver.Query(uri, projection, null, null, null); 

    if (cursor.MoveToFirst()) 
    { 
     do 
     { 
      string id = cursor.GetString(cursor.GetColumnIndex(projection[0])); 
      string name = cursor.GetString(cursor.GetColumnIndex(projection[1])); 
      string photoUri = cursor.GetString(cursor.GetColumnIndex(projection[2])); 
      contacts.Add(new Contact() { Id = long.Parse(id), DisplayName = name, PhotoUri = photoUri }); 
     } while (cursor.MoveToNext()); 

     GetContactPhoneNumber(contacts); 
    } 

    return contacts; 
} 

private async void GetContactPhoneNumber(List<Contact> list) 
{ 
    string[] projection = 
     { 
        ContactsContract.CommonDataKinds.Phone.Number, 
        ContactsContract.CommonDataKinds.Phone.InterfaceConsts.ContactId 
       }; 

    var cursor = ContentResolver.Query(ContactsContract.CommonDataKinds.Phone.ContentUri, projection, null, null, null); 
    if (cursor.Count > 0) 
    { 
     await Task.Factory.StartNew(() => 
     { 
      do 
      { 
       try 
       { 
        string id = cursor.GetString(cursor.GetColumnIndex(projection[1])); 
        string phoneNumber = cursor.GetString(cursor.GetColumnIndex(projection[0])); 
        Contact contact = list.Where(c => c.Id == long.Parse(id)).FirstOrDefault(); 
        contact.PhoneNumber = phoneNumber; 
       } 
       catch 
       { 
       } 

      } while (cursor.MoveToNext()); 
      cursor.Close(); 
     }); 
    } 
} 
Verwandte Themen