0

I am Trying to access all Name & Phone Number from Contacts. I have tried different methods and I am only getting Name and I am not able to get Phone Number on my Moto E3 devices.

In Some Devices it is working fine.. but not on my Moto E3 devices because of that I thought my Code is not working.. I dont know Why it is not Showing Phone Number on Moto E3 Devices..

I known this is a basic question or may be duplicate but the previous answer which I have seen are not much helpful for me.

Refred Links are: Link1 Link2

int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));

hasPhoneNumber variable is always giving me Zero value(0).

ANSWER

Then I checked in Contacts App of Moto E3 devices, the Contacts are where added from SKYPE.. So there where no Contacts from Google Id or Sim Card.

Because of that I am not able to get the Contacts in My Moto E3. So, I added some Numbers in my Google Id and then Sync the Contacts.. Then I fetch the data. I got all the Added Contacts..

Main.java

private void getContactList() {
    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

    if (cursor.getCount() > 0) {
        while (cur != null && cur.moveToNext()) {

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

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

            int hasPhoneNumber = cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

            if (hasPhoneNumber > 0) //hasPhoneNumber is always give Zero value(0)
            {
                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                        new String[]{id}, null);
                while (pCur.moveToNext()) {
                    String phoneNo = pCur.getString(pCur.getColumnIndex(
                            ContactsContract.CommonDataKinds.Phone.NUMBER));
                    Log.i("TAG", "Name: " + name);
                    Log.i("TAG", "Phone Number: " + phoneNo);
                }
                pCur.close();

            }
        }
    }
    if(cur!=null){
        cur.close();
    }
}

I want all Names & Phone Numbers from Contacts Please Help me..

Thank You..

NOTE : I have Checked The above code is working fine in another Devices. But not on my Moto E3 devices because that I thought my Code is not working.. I dont know Why it not Showing Phone NUmber on Moto E3 Devices.

Fra Red
  • 360
  • 3
  • 13
  • Did you go through the android documents already for this? – amitava Mar 03 '19 at 12:15
  • @amitava thanks for your Reply.. Now I have Checked The above code is working fine. I have checked in another devices it is showing Phone number but not on my **Moto E3** devices because that I thought my Code is not working.. I dont know Why it not Showing Phone NUmber on Moto E3 Devices.. – Fra Red Mar 03 '19 at 12:53
  • Now I got the solution that My Contact Numbers Where not sysnc.. When I select the google Id or Sim in Contacts App it is giving the Output.. – Fra Red Mar 03 '19 at 13:23
  • i've marked is a dup of https://stackoverflow.com/questions/46572169 my answer there should cover your request as well, you just need to modify the `selection` to include only the mimetypes you're interested in – marmor Mar 04 '19 at 08:20
  • @FraRed, go through this https://stackoverflow.com/questions/53956496/android-not-getting-any-contacts-from-contact-list/53958619#53958619 – ॐ Rakesh Kumar Mar 05 '19 at 07:09

0 Answers0