I am trying to use provider ContactsContract
for retrieving contacts information. Before fetching the Phone Numbers
of contacts, I am checking whether contacts have phone numbers. For that I am using uri ContactsContract.Contacts.HAS_PHONE_NUMBER
but on some devices it returns 1 or 0
and returns NULL
on others. I am testing on Android 2.3.1
, it returns NULL
here. When tried to debug, String phonenumber = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
, It shows the mobile number. Can anyone please point out why the behaviour is different. I have checked the API of ContactsContract
, its level 5
which means devices 2.3+
can use this. I am getting the following exception:
Exception
06-03 13:01:51.065: E/AndroidRuntime(545): java.lang.RuntimeException:
Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { dat=content://com.android.contacts/data/2 flg=0x1 (has extras) }}
to activity {com.tzoomers.birthdaysdiary/com.tzoomers.birthdaysdiary.BirthdaysDiary}:
java.lang.IllegalStateException: get field slot from row 0 col -1 failed
Start Contacts Picker Activity
private void addFromContacts()
{
Intent contactIntent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds
.Phone.CONTENT_URI);
this.context.startActivityForResult(contactIntent, CONTACT_SELECTED);
}
onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode)
{
case CONTACT_SELECTED:
if(resultCode == Activity.RESULT_OK)
{
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
int cId;
String phoneNumber = "";
String name = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
cId = c.getInt(c.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
int hasPhone = Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
if(hasPhone > 0)
{
phoneNumber = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
}
}
break;
}
}