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.
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.