从android联系人列表中获取电子邮件

时间:2011-01-21 13:36:49

标签: android

如何在Android联系人中以编程方式获取选定的人员电子邮件ID

应支持所有版本。我尝试使用人类,但不推荐使用主电子邮件方法

提前致谢

阿斯旺

2 个答案:

答案 0 :(得分:2)

嘿阿斯旺 我从来没有这样做但可以给你一点想法。

此代码可让您了解如何将电子邮件添加到联系人中。

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");  

然后你将有2个查询这个游标。 现在我认为你将有2个改变投影。您必须添加在上面的代码中添加电子邮件时使用的相同常量。

你可以获得所有这些东西here

答案 1 :(得分:1)

对于5之前的API版本,您必须使用Contacts类,而对于5以后,您必须使用ContactsContract类。

您必须查询API版本,然后决定使用哪个类。