我该如何修改下面的代码,以便它从Android联系人中抓取姓名(它已经这样做了)和手机号码呢? 我想在联系人中把电话号码标记为移动电话。
private ArrayList<ContactInfo> getLegalContacts() {
ArrayList<ContactInfo> legalContacts = new ArrayList<>();
List<String> contacts = loadLegalContacts();
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
while (phones.moveToNext()) {
String id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
if (phoneNumber != null) {
phoneNumber = phoneNumber.replace("(", "");
phoneNumber = phoneNumber.replace(")", "");
phoneNumber = phoneNumber.replace("-", "");
phoneNumber = phoneNumber.replace(" ", "");
phoneNumber = phoneNumber.replace("+", "");
}
if (contacts.contains(id)) {
ContactInfo info = new ContactInfo(id, name, phoneNumber, true);
boolean isNew = true;
for (int i = 0; i < legalContacts.size(); i++) {
if (TextUtils.equals(legalContacts.get(i).name, name)) {
isNew = false;
break;
}
}
if (isNew) {
legalContacts.add(info);
}
}
}
phones.close();
return legalContacts;
}
private List<String> loadLegalContacts() {
List<String> contacts = new ArrayList<>();
Set<String> set = SharedPrefUtil.getInstance().getStringSet("LegalContacts");
if (set != null) {
contacts.addAll(set);
}
return contacts;
}
你可以这样做
private ArrayList<ContactInfo> getLegalContacts() {
ArrayList<ContactInfo> legalContacts = new ArrayList<>();
List<String> contacts = loadLegalContacts();
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
while (phones.moveToNext()) {
String id = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String labelString = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
if(labelString!=null)
{
int type = Integer.parseInt(labelString);
if(type==ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) {
Log.d("LableTag", " lable is : " + type);
if (phoneNumber != null) {
phoneNumber = phoneNumber.replace("(", "");
phoneNumber = phoneNumber.replace(")", "");
phoneNumber = phoneNumber.replace("-", "");
phoneNumber = phoneNumber.replace(" ", "");
phoneNumber = phoneNumber.replace("+", "");
}
if (contacts.contains(id)) {
ContactInfo info = new ContactInfo(id, name, phoneNumber, true);
boolean isNew = true;
for (int i = 0; i < legalContacts.size(); i++) {
if (TextUtils.equals(legalContacts.get(i).getName(), name)) {
isNew = false;
break;
}
}
if (isNew) {
legalContacts.add(info);
}
}
}
}
}
phones.close();
return legalContacts;
}
labelString
将保存当前标记为
然后我们需要检查它是否是移动类型,如果为真,那么我们访问它并将其添加到list中
有关更多信息,请访问contactscontract.commondatakinds.phone