Improving @Adiii's answer - this will clear the phone number and remove all duplicates
Declare a global variable
// Hash Maps Map<String, String> namePhoneMap = new HashMap<String, String>();
Then use the function below
private void getPhoneNumbers() { Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); // Loop Through All The Numbers while (phones.moveToNext()) { String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); // Cleanup the phone number phoneNumber = phoneNumber.replaceAll("[()\\s-]+", ""); // Enter Into Hash Map namePhoneMap.put(phoneNumber, name); } // Get The Contents of Hash Map in Log for (Map.Entry<String, String> entry : namePhoneMap.entrySet()) { String key = entry.getKey(); Log.d(TAG, "Phone :" + key); String value = entry.getValue(); Log.d(TAG, "Name :" + value); } phones.close(); }
Remember that in the above example, the key is the phone number and the value is the name, so read the contents, for example, 998xxxxx282-> Mahatma Gandhi instead of Mahatma Gandhi-> 998xxxxx282
DragonFire Jan 17 '19 at 2:37 2019-01-17 02:37
source share