开发者

get email from android contact list

how to get selected person email id programmatically in android contacts

that should support all version.i tried with people class bu开发者_运维百科t there primary email method is deprecated

Thanks in advance

Aswan


Hey Aswan I have never done this but can give u idea.

This code gives u 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, "test@example.com");  
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 u will have 2 query this cursor. Now i think that u will have 2 make changes projection. You will have to add the same constant that u had used while adding email in the above code.

You can get all this stuff here.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜