Home » Android » contact – get email type and phone number type 's label used android

contact – get email type and phone number type 's label used android

Posted by: admin May 14, 2020 Leave a comment

Questions:

I want to fetch the type of email type and phone number type's label used but when im fetching the data using these code its giving position of label use means return integer value but i want the label used.

Where i am wrong in my code?

Success full getting email id but for type giving int. value as 1,2.

So how to get label for type?

public String [] getEmailid(long _id) {
    String emailid = null ;
    String emailType = null ;
    try {
        Cursor cursor = getContentResolver().query(  
            ContactsContract.CommonDataKinds.Email.CONTENT_URI,  
            new String[]{Email.DATA,Email.TYPE},
            ContactsContract.CommonDataKinds.Email.CONTACT_ID +" = "+ _id, 
            // We need to add more selection for phone type
            null,
            null);

        if(cursor != null) {
            while (cursor.moveToNext()) {  
                // This would allow you get several email addresses  
                // if the email addresses were stored in an array  
                // Log.i("RETURN EMAIL TYPA",emailid);
                emailid = cursor.getString(cursor.getColumnIndex(Email.DATA)); 
                emailType = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));  

                // TODO Auto-generated method stub
                if(emailid != null)
                    break;
            }
        }
    }
//.....
How to&Answers:

Common types (home, work, etc.) are stored as ints. This avoids keeping redundant strings in the database and allows for localization. You can look up the localized string for the common type’s int with the following API:

http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.Email.html#getTypeLabel(android.content.res.Resources,%20int,%20java.lang.CharSequence)

Note that when the type is TYPE_CUSTOM, you need to provide the custom label. Here is an example:

int type = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
String customLabel = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.LABEL));
CharSequence emailType = ContactsContract.CommonDataKinds.Email.getTypeLabel(context.getResources(), type, customLabel);

Answer:

try below code. i am able to get Label with phone number.

Uri uri = ContactsContract.Contacts.CONTENT_URI;
        ContentResolver cr = getContentResolver();
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor cur=cr.query(uri, null, null, null, sortOrder);
        if(cur.getCount()>0){
        while(cur.moveToNext()){


            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))> 0) {
//get the phone number
                    Cursor phoneCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                      ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);
                    while (phoneCur.moveToNext()) {
                   String phoneNumber= phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    int phonetype = phoneCur.getInt(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
                    String customLabel = phoneCur.getString(phoneCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL));
                   String phoneLabel = (String) ContactsContract.CommonDataKinds.Email.getTypeLabel(this.getResources(), phonetype, customLabel);                       
                    Log.e(TAG, "Phone Number: " + phoneNumber + " Selected Phone Label: " + phoneLabel);
                                        }phoneCur.close();
}              
            }
        } cur.close();   

Answer:

type = cursor.getInt(cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.TYPE));

getActivity().getResources().getString(ContactsContract.CommonDataKinds.Phone.getTypeLabelResource(type))