我尝试获取所有电话联系人,将代码放在下面,它可以正常工作,一切都很好,但是它不能撤退所有联系人,大约一半的联系人都撤退了。
光标游标= getContentResolver()。query(ContactsContract.Contacts.CONTENT_URI, 空值, 空值, 空值, null);
if (cursor == null) {
return;
}
cursor.moveToFirst();
do {
String name = cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String id = cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts.NAME_RAW_CONTACT_ID));
Cursor phones = getContentResolver()
.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
if (phones != null) {
while (phones.moveToNext()) { // loop for number of a single contact
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String ModifiedPhoneNumber = "";
for(int i=0;i<phoneNumber.length();i++){
char c = phoneNumber.charAt(i);
if(c != '(' && c != ')' && c != '-' && c != ' '){
ModifiedPhoneNumber += c;
}
}
CollectionReference db_contacts = firestore_ref().collection("contacts");
HashMap contactModel = new HashMap();
contactModel.put("name", name);
contactModel.put("phone_number", ModifiedPhoneNumber);
db_contacts.add(contactModel);
}
phones.close();
}
} while (cursor.moveToNext());
我希望从应用程序中获取所有联系人,在任何真实设备上都能获取100%的联系人