2011-01-21 52 views

回答

1

對於API版本5以前的版本,您必須使用Contacts類,對於5以後的版本,您必須使用ContactsContract類。

您必須查詢API版本,然後決定使用哪個類。

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

那麼你將有2查詢這個遊標。 現在我認爲你將有2個改變投影。在上面的代碼中添加電子郵件時,您必須添加與您使用的相同的常量。

你可以得到所有這些東西here

相關問題