Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get email from android contact list

Tags:

android

how to get selected person email id programmatically in android contacts

that should support all version.i tried with people class but there primary email method is deprecated

Thanks in advance

Aswan

like image 898
Aswan Avatar asked Mar 08 '26 21:03

Aswan


2 Answers

I have never done this but can give you an idea.

This code gives you an idea about putting email add in a contact.

import android.provider.Contacts.People;
import android.content.ContentResolver;
import android.content.ContentValues; 

ContentValues values = new ContentValues();

// Add Abraham Lincoln to contacts and make him a favorite.  
values.put(People.NAME, "Abraham Lincoln");  
// 1 = the new contact is added to favorites  
// 0 = the new contact is not added to favorites  
values.put(People.STARRED, 1);  
 
Uri uri = getContentResolver().insert(People.CONTENT_URI, values);     
Uri phoneUri = null;  
Uri emailUri = null;  

// Add a phone number for Abraham Lincoln.  Begin with the URI for  
// the new record just returned by insert(); it ends with the _ID  
// of the new record, so we don't have to add the ID ourselves.  
// Then append the designation for the phone table to this URI,  
// and use the resulting URI to insert the phone number.  
phoneUri = Uri.withAppendedPath(uri, People.Phones.CONTENT_DIRECTORY);  

values.clear();  
values.put(People.Phones.TYPE, People.Phones.TYPE_MOBILE);  
values.put(People.Phones.NUMBER, "1233214567");  
getContentResolver().insert(phoneUri, values);  
  
// Now add an email address in the same way.  
emailUri = Uri.withAppendedPath(uri, People.ContactMethods.CONTENT_DIRECTORY);  
  
values.clear();  
// ContactMethods.KIND is used to distinguish different kinds of  
// contact methods, such as email, IM, etc.   
values.put(People.ContactMethods.KIND, Contacts.KIND_EMAIL);  
values.put(People.ContactMethods.DATA, "[email protected]");  
values.put(People.ContactMethods.TYPE, People.ContactMethods.TYPE_HOME);  
getContentResolver().insert(emailUri, values);     


now you have to extract emaill from contact

import android.provider.Contacts.People;  
import android.database.Cursor;  

// Form an array specifying which columns to return.   
String[] projection = new String[] {  
                             People._ID,  
                             People._COUNT,  
                             People.NAME,  
                             People.NUMBER  
                          };  

// Get the base URI for the People table in the Contacts content provider.  
Uri contacts =  People.CONTENT_URI;  

// Make the query.   
Cursor managedCursor = managedQuery(contacts,   
                         projection, // Which columns to return    
                         null,       // Which rows to return (all rows)  
                         null,       // Selection arguments (none)  
                         // Put the results in ascending order by name  
                         People.NAME + " ASC");  

Then you will have to query this cursor. Now I think that you will have to make changes projection. You will have to add the same constant that you had used while adding email in the above code.

You can get all this stuff here.

like image 155
N-JOY Avatar answered Mar 14 '26 07:03

N-JOY


For API versions prior 5 you have to use the Contacts class and for 5 onwards you have to use the ContactsContract class.

You have to query the API version and upon that decide which class to use.

like image 22
Octavian A. Damiean Avatar answered Mar 14 '26 07:03

Octavian A. Damiean



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!