Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
            }
        }
    }
//.....
like image 616
ram ji Avatar asked Aug 04 '11 05:08

ram ji


2 Answers

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();   
like image 184
Emran Hamza Avatar answered Sep 21 '22 14:09

Emran Hamza


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);
like image 34
Mason Lee Avatar answered Sep 18 '22 14:09

Mason Lee