基本上我正在尝试获取所有具有电话号码和/或电子邮件地址的联系人。我的日历只与Google同步,因为我的电话号码在那里,但问题是我也收到了一些电子邮件地址和重复的联系人,例如,我有一个电话号码并使用Whatsapp,因此它是向我显示我的电话号码的2个条目。以下是我正在使用的一些代码:
private void queryAllRawContacts() {
final String[] projection = new String[] {
RawContacts.CONTACT_ID, // the contact id column
RawContacts.DELETED // column if this contact is deleted
};
final Cursor rawContacts = managedQuery(
RawContacts.CONTENT_URI, // the uri for raw contact provider
projection,
null, // selection = null, retrieve all entries
null, // not required because selection does not contain parameters
null); // do not order
final int contactIdColumnIndex = rawContacts.getColumnIndex(RawContacts.CONTACT_ID);
final int deletedColumnIndex = rawContacts.getColumnIndex(RawContacts.DELETED);
spinnerContent.clear();
if(rawContacts.moveToFirst()) { // move the cursor to the first entry
while(!rawContacts.isAfterLast()) { // still a valid entry left?
final int contactId = rawContacts.getInt(contactIdColumnIndex);
final boolean deleted = (rawContacts.getInt(deletedColumnIndex) == 1);
if(!deleted) {
spinnerContent.add(queryDetailsForContactSpinnerEntry(contactId));
}
rawContacts.moveToNext(); // move to the next entry
}
}
rawContacts.close();
}
public void queryAllPhoneNumbersForContact(int contactId, List<ListViewEntry> content) {
final String[] projection = new String[] {
Phone.NUMBER,
Phone.TYPE,
};
final Cursor phone = managedQuery(
Phone.CONTENT_URI,
projection,
Data.CONTACT_ID + "=?",
new String[]{String.valueOf(contactId)},
null);
if(phone.moveToFirst()) {
final int contactNumberColumnIndex = phone.getColumnIndex(Phone.NUMBER);
final int contactTypeColumnIndex = phone.getColumnIndex(Phone.TYPE);
while(!phone.isAfterLast()) {
final String number = phone.getString(contactNumberColumnIndex);
final int type = phone.getInt(contactTypeColumnIndex);
content.add(new ListViewEntry(number, Phone.getTypeLabelResource(type),R.string.type_phone));
phone.moveToNext();
}
}
phone.close();
}
public void queryAllEmailAddressesForContact(int contactId, List<ListViewEntry> content) {
final String[] projection = new String[] {
Email.DATA, // use Email.ADDRESS for API-Level 11+
Email.TYPE
};
final Cursor email = managedQuery(
Email.CONTENT_URI,
projection,
Data.CONTACT_ID + "=?",
new String[]{String.valueOf(contactId)},
null);
if(email.moveToFirst()) {
final int contactEmailColumnIndex = email.getColumnIndex(Email.DATA);
final int contactTypeColumnIndex = email.getColumnIndex(Email.TYPE);
while(!email.isAfterLast()) {
final String address = email.getString(contactEmailColumnIndex);
final int type = email.getInt(contactTypeColumnIndex);
content.add(new ListViewEntry(address, Email.getTypeLabelResource(type),R.string.type_email));
email.moveToNext();
}
}
email.close();
}