如何修改Android代码以获取联系人姓名及标注为移动的电话号码?
Solution: Filter Only Mobile Phone Numbers from Android Contacts
Got it, let's adjust your code to exclusively pull mobile phone numbers while keeping all your existing valid contact logic (like filtering legal contacts and avoiding duplicate names) intact. Here's the modified code and a breakdown of the key changes:
Modified Full Code
private ArrayList<ContactInfo> getLegalContacts() { ArrayList<ContactInfo> legalContacts = new ArrayList<>(); List<String> contacts = loadLegalContacts(); // Add selection to filter ONLY mobile phone numbers String selection = ContactsContract.CommonDataKinds.Phone.TYPE + " = ?"; String[] selectionArgs = new String[]{String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)}; Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, selection, selectionArgs, 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)); // Keep your existing phone number formatting logic if (phoneNumber != null) { phoneNumber = phoneNumber.replace("(", ""); phoneNumber = phoneNumber.replace(")", ""); phoneNumber = phoneNumber.replace("-", ""); phoneNumber = phoneNumber.replace(" ", ""); phoneNumber = phoneNumber.replace("+", ""); } // Only add if the contact is in your legal contacts list 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; }
Key Changes Explained
- Filter Mobile Numbers at Query Time: We added a
selectionparameter to thequery()call to only fetch records where the phone number type isTYPE_MOBILE. This is more efficient than fetching all numbers and filtering later, as it reduces the amount of data we process from the cursor. - Preserved Existing Logic: All your original code for formatting phone numbers, checking against the legal contacts list, and avoiding duplicate names remains untouched—so your existing functionality works exactly as before, just with mobile-only numbers.
Optional: Include Other Mobile Types
If you need to include additional mobile-related number types (like work mobile phones), update the selection logic to use an IN clause:
String selection = ContactsContract.CommonDataKinds.Phone.TYPE + " IN (?, ?)"; String[] selectionArgs = new String[]{ String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE), String.valueOf(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE_WORK) };
内容的提问来源于stack exchange,提问作者NewDroid
相关产品推荐
相关产品推荐

