我正在尝试获取与Xamarin.Android联系的所有电话号码和电子邮件。我找到了这个https://stackoverflow.com/a/2356760/4965222但是我不能将这个原始的android配方应用于Xamarin.Android,因为找不到我可以获得Phones._ID
,Phones.TYPE
,Phones.NUMBER
,{{1 },Phones.LABEL
。如何在没有Xamarin.Mobile库的情况下获取此数据?
答案 0 :(得分:1)
这是起点
public List<PersonContact> GetPhoneContacts()
{
var phoneContacts = new List<PersonContact>();
using (var phones = ApplicationContext.ContentResolver.Query(ContactsContract.CommonDataKinds.Phone.ContentUri, null, null, null, null))
{
if (phones != null)
{
while (phones.MoveToNext())
{
try
{
string name = phones.GetString(phones.GetColumnIndex(ContactsContract.Contacts.InterfaceConsts.DisplayName));
string phoneNumber = phones.GetString(phones.GetColumnIndex(ContactsContract.CommonDataKinds.Phone.Number));
string[] words = name.Split(' ');
PersonContact contact = new PersonContact();
contact.FirstName = words[0];
if (words.Length > 1)
contact.LastName = words[1];
else
contact.LastName = ""; //no last name, is that ok?
contact.PhoneNumber = phoneNumber;
phoneContacts.Add(contact);
}
catch (Exception ex)
{
//something wrong with one contact, may be display name is completely empty, decide what to do
}
}
phones.Close(); //not really neccessary, we have "using" above
}
//else we cannot get to phones, decide what to do
}
return phoneContacts;
}
public class PersonContact
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string PhoneNumber { get; set; }
}
答案 1 :(得分:1)
经过文献研究,我找到了答案。
//filtering phones related to a contact
var phones = Application.Context.ContentResolver.Query(
ContactsContract.CommonDataKinds.Phone.ContentUri,
null,
ContactsContract.CommonDataKinds.Phone.InterfaceConsts.ContactId
+ " = " + contactId, null, null);
// getting phone numbers
while (phones.MoveToNext())
{
var number =
phones.GetString( //specify which column we want to get
phones.GetColumnIndex(ContactsContract.CommonDataKinds.Phone.Number));
// do work with number
}
phones.Close();