Android not detecting contacts synced from other accounts programmatically

I am trying to get list of all contacts

Here is the code

ContentResolver cr = mContext.getContentResolver();
            Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,  null, null, null, null);
            if (cur.getCount() > 0)
            {
                while (cur.moveToNext())
                {
                    ContactsModel contact = new ContactsModel();

                    contact.id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                    contact.name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    if (contact.name.equals("Joseph"))
                    {
                        Log.d("TAG", "THAT'S THE GUY");
                    }

                    if (Integer.parseInt(cur.getString(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[]{contact.id}, null);

                        boolean isMobileDetected = false;

                        while (pCur.moveToNext())
                        {
                            if(!isMobileDetected)
                            {
                                int phoneType = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                                String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                switch (phoneType)
                                {
                                    case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                                        contact.phoneNumber = phoneNo;
                                        break;

                                    case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                                        contact.phoneNumber = phoneNo;
                                        break;

                                    case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE:
                                        contact.phoneNumber = phoneNo;
                                        isMobileDetected = true;
                                        break;

                                    case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                                        contact.phoneNumber = phoneNo;
                                        isMobileDetected = true;
                                        break;
                                }
                            }
                        }
                        pCur.close();
                        /* We will fix number. All users who have international numbers will always put their country
                            codes
                         */
                        String phone = fixPhoneNumber(mContext, contact.phoneNumber);
                        if (!phone.equals(""))
                        {
                            contact.phoneNumber = phone;
                            contacts.add(contact);
                        }
                    }
                }
            }

            Collections.sort(contacts, new ContactsComparator());

The problem with this code is, its skipping few contacts.
E.g here is a contact
enter image description here

As can see the contact is in contacts list, here i get a Jospeh but with his real name , not the one shown in contact Also it doesn’t detect its phone using

                if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)

What is going on here. The contact maybe synced from Google account to display its real name but why am i not getting the phone number?


Source: eclipse

Leave a Reply