Android, I am trying to find what type of number (from contact) (mobile, home, work)
String tmp = txtPhoneName.getText().toString();
ContentResolver cr = getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, "DISPLAY_NAME = '" + tmp + "'", null, null);
if (cursor.moveToFirst()) {
String contactId = Cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor phones = cr.query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
number1 = number;
int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));
switch (type) {
case Phone.TYPE_HOME:
// do something with the Home number here...
break;
case Phone.TYPE_MOBILE:
// do something with the Mobile number here...
break;
case Phone.TYPE_WORK:
// do something with the Work number here...
break;
}
}
phones.close();
}
My problem is that when it is in switch status it doesn't check anything. It jumps to while (phones.moveToNext ()), where my mistake makes Phone.TYPE_HOME not int and my type cannot match it ... And the last question is why for some contacts it gets the mobile number and for others - at home (and in both cases the contacts have a home and mobile number)? I want to get a number starting with 07, and if there is no such number to get the number that is for the contact and then how to call that number ... thanks
+3
source to share
3 answers
Here I am going to show you how to get a home, mobile and work phone without any contacts. First of all you will get the uri of any contact_id and then use the method below to get all the phone numbers.
while (phone_crsr.moveToNext())
{
int phone_type = phone_crsr.getInt(phone_crsr.getColumnIndex(Phone.TYPE));
switch (phone_type)
{
case Phone.TYPE_HOME:
phone_home =phone_crsr.getString(phone_crsr.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(this, "home"+phone_home, Toast.LENGTH_LONG).show();
break;
case Phone.TYPE_MOBILE:
phone_mob=phone_crsr.getString(phone_crsr.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(this, "mob"+phone_mob, Toast.LENGTH_LONG).show();
break;
case Phone.TYPE_WORK:
phone_work=phone_crsr.getString(phone_crsr.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(this, "work"+phone_work, Toast.LENGTH_LONG).show();
break;
}
}
+9
source to share