-
-
Save nivalamata/239efd0d40cf10ca2337148ae8cd735c to your computer and use it in GitHub Desktop.
Android - Get phone number and email address from Contacts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private class FetchContacts extends AsyncTask<Void, Void, ArrayList<Contact>> { | |
| private final String DISPLAY_NAME = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? | |
| ContactsContract.Contacts.DISPLAY_NAME_PRIMARY : ContactsContract.Contacts.DISPLAY_NAME; | |
| private final String FILTER = DISPLAY_NAME + " NOT LIKE '%@%'"; | |
| private final String ORDER = String.format("%1$s COLLATE NOCASE", DISPLAY_NAME); | |
| @SuppressLint("InlinedApi") | |
| private final String[] PROJECTION = { | |
| ContactsContract.Contacts._ID, | |
| DISPLAY_NAME, | |
| ContactsContract.Contacts.HAS_PHONE_NUMBER | |
| }; | |
| @Override | |
| protected ArrayList<LearnSaveContact> doInBackground(Void... params) { | |
| try { | |
| ArrayList<Contact> contacts = new ArrayList<>(); | |
| ContentResolver cr = getContentResolver(); | |
| Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, PROJECTION, FILTER, null, ORDER); | |
| if (cursor != null && cursor.moveToFirst()) { | |
| do { | |
| // get the contact's information | |
| String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); | |
| String name = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME)); | |
| Integer hasPhone = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); | |
| // get the user's email address | |
| String email = null; | |
| Cursor ce = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, | |
| ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null); | |
| if (ce != null && ce.moveToFirst()) { | |
| email = ce.getString(ce.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); | |
| ce.close(); | |
| } | |
| // get the user's phone number | |
| String phone = null; | |
| if (hasPhone > 0) { | |
| Cursor cp = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, | |
| ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null); | |
| if (cp != null && cp.moveToFirst()) { | |
| phone = cp.getString(cp.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); | |
| cp.close(); | |
| } | |
| } | |
| // if the user user has an email or phone then add it to contacts | |
| if ((!TextUtils.isEmpty(email) && android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches() | |
| && !email.equalsIgnoreCase(name)) || (!TextUtils.isEmpty(phone))) { | |
| Contact contact = new Contact(); | |
| contact.name = name; | |
| contact.email = email; | |
| contact.phoneNumber = phone; | |
| contacts.add(contact); | |
| } | |
| } while (cursor.moveToNext()); | |
| // clean up cursor | |
| cursor.close(); | |
| } | |
| return contacts; | |
| } catch (Exception ex) { | |
| return null; | |
| } | |
| } | |
| @Override | |
| protected void onPostExecute(ArrayList<Contact> contacts) { | |
| if (contacts != null) { | |
| // success | |
| mContacts = contacts; | |
| } else { | |
| // show failure | |
| // syncFailed(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment