How to access contacts

Viewed 50

Picking contacts is similar to opening gallery and picking a picture but how do I access all contacts and relate it to my app. For practice i'm trying to create a basic chat app so I don't want the users signed up to be shown in my activity but I want to show only the users who are in my contacts and who are using my app just like whatsapp, wechat or any other texting app. Also I was thinking whether I could access Favorites just like Contacts... How can I achieve this?

2 Answers

For retrieving all contacts you could use something like this:

    final ArrayList<Contact> contacts = new ArrayList<>();
    ContentResolver contentResolver = getContentResolver();
    Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
    if (cursor != null) {
        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {
                int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
                if (hasPhoneNumber > 0) {
                    String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    Cursor phoneCursor = contentResolver.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id},
                            null);
                    if (phoneCursor != null) {
                        if (phoneCursor.moveToNext()) {
                            String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                            contacts.add(new Contact(name, phoneNumber));
                            phoneCursor.close();
                        }
                    }
                }
            }
        }

        cursor.close();
    }

For getting the only ones that are using the chat, maybe you should save that info in the server, using the phone number as id, for example. And when you retrieve all the contacts you could compare the phone numbers and add to the array only the ones that are registered in your server...

    ContentResolver cr = context.getApplicationContext().getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);

    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {

            String id;
            String name;
            String phoneNumbers = "";

            id = cur.getString(
                    cur.getColumnIndex(ContactsContract.Contacts._ID));
            name = cur.getString(cur.getColumnIndex(
                    ContactsContract.Contacts.DISPLAY_NAME));

            if (cur.getInt(cur.getColumnIndex(
                    ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                        new String[]{id}, null);
                while (pCur.moveToNext()) {
                    String pn = pCur.getString(pCur.getColumnIndex(
                            ContactsContract.CommonDataKinds.Phone.NUMBER));

                    if (pCur.isFirst())
                        phoneNumbers += pn;
                    else
                        phoneNumbers += ", " + pn;


                }
                pCur.close();
            }
        }
    }

    cur.close();
Related