下面列出了怎么用android.provider.ContactsContract.CommonDataKinds.StructuredName的API类实例代码及写法,或者点击链接到github查看源代码。
private void addNameData(WritableMap contactData, Cursor cursor) {
int displayNameIndex = cursor.getColumnIndex(StructuredName.DISPLAY_NAME);
contactData.putString("name", cursor.getString(displayNameIndex));
int givenNameColumn = cursor.getColumnIndex(StructuredName.GIVEN_NAME);
if (givenNameColumn != -1) {
String givenName = cursor.getString(givenNameColumn);
contactData.putString("givenName", givenName);
}
int familyNameColumn = cursor.getColumnIndex(StructuredName.FAMILY_NAME);
if (familyNameColumn != -1) {
String familyName = cursor.getString(cursor.getColumnIndex(StructuredName.FAMILY_NAME));
contactData.putString("familyName", familyName);
}
int middleNameColumn = cursor.getColumnIndex(StructuredName.MIDDLE_NAME);
if (middleNameColumn != -1) {
String middleName = cursor.getString(middleNameColumn);
contactData.putString("middleName", middleName);
}
}
/**
* Adds a contact name. We can take either a full name ("Bob Smith") or separated
* first-name and last-name ("Bob" and "Smith").
*
* @param fullName The full name of the contact - typically from an edit form
* Can be null if firstName/lastName are specified.
* @param firstName The first name of the contact - can be null if fullName
* is specified.
* @param lastName The last name of the contact - can be null if fullName
* is specified.
* @return instance of ContactOperations
*/
ContactOperations addName(final String fullName, final String firstName, final String lastName) {
mValues.clear();
if (!TextUtils.isEmpty(fullName)) {
mValues.put(StructuredName.DISPLAY_NAME, fullName);
mValues.put(StructuredName.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
} else {
if (!TextUtils.isEmpty(firstName)) {
mValues.put(StructuredName.GIVEN_NAME, firstName);
mValues.put(StructuredName.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
}
if (!TextUtils.isEmpty(lastName)) {
mValues.put(StructuredName.FAMILY_NAME, lastName);
// It's OK to add the same value again.
mValues.put(StructuredName.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
}
}
if (mValues.size() > 0) {
addInsertOp();
}
return this;
}
/**
* Updates contact's name. The caller can either provide first-name
* and last-name fields or a full-name field.
*
* @param uri Uri for the existing raw contact to be updated
* @param existingFirstName the first name stored in provider
* @param existingLastName the last name stored in provider
* @param existingFullName the full name stored in provider
* @param firstName the new first name to store
* @param lastName the new last name to store
* @param fullName the new full name to store
* @return instance of ContactOperations
*/
ContactOperations updateName(Uri uri,
String existingFirstName,
String existingLastName,
String existingFullName,
String firstName,
String lastName,
String fullName) {
mValues.clear();
if (TextUtils.isEmpty(fullName)) {
if (!TextUtils.equals(existingFirstName, firstName)) {
mValues.put(StructuredName.GIVEN_NAME, firstName);
}
if (!TextUtils.equals(existingLastName, lastName)) {
mValues.put(StructuredName.FAMILY_NAME, lastName);
}
} else {
if (!TextUtils.equals(existingFullName, fullName)) {
mValues.put(StructuredName.DISPLAY_NAME, fullName);
}
}
if (mValues.size() > 0) {
addUpdateOp(uri);
}
return this;
}
/**
* Synchronously insert a contact with the designated @name into
* the ContactsContentProvider. This code is explained at
* http://developer.android.com/reference/android/provider/ContactsContract.RawContacts.html.
*/
private void addContact(String name,
List<ContentProviderOperation> cpops) {
final int position = cpops.size();
// First part of operation.
cpops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE,
mOps.getAccountType())
.withValue(RawContacts.ACCOUNT_NAME,
mOps.getAccountName())
.withValue(Contacts.STARRED,
1)
.build());
// Second part of operation.
cpops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID,
position)
.withValue(Data.MIMETYPE,
StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.DISPLAY_NAME,
name)
.build());
}
/**
* Synchronously insert a contact with the designated @name into
* the ContactsContentProvider. This code is explained at
* http://developer.android.com/reference/android/provider/ContactsContract.RawContacts.html.
*/
private void addContact(String name,
List<ContentProviderOperation> cpops) {
final int position = cpops.size();
// First part of operation.
cpops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE,
mOps.getAccountType())
.withValue(RawContacts.ACCOUNT_NAME,
mOps.getAccountName())
.withValue(Contacts.STARRED,
1)
.build());
// Second part of operation.
cpops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID,
position)
.withValue(Data.MIMETYPE,
StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.DISPLAY_NAME,
name)
.build());
}
private void addRecordToBatchInsertOperation(String name,
List<ContentProviderOperation> ops) {
int position = ops.size();
// First part of operation
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, mType)
.withValue(RawContacts.ACCOUNT_NAME, mName)
.withValue(Contacts.STARRED, 1).build());
// Second part of operation
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, position)
.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.DISPLAY_NAME, name).build());
}
private void addRecordToBatchInsertOperation(String name,
List<ContentProviderOperation> ops) {
int position = ops.size();
// First part of operation
ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
.withValue(RawContacts.ACCOUNT_TYPE, mType)
.withValue(RawContacts.ACCOUNT_NAME, mName)
.withValue(Contacts.STARRED, 1).build());
// Second part of operation
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
.withValueBackReference(Data.RAW_CONTACT_ID, position)
.withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.DISPLAY_NAME, name).build());
}
/**
* 往手机通讯录插入联系人
*
* @param ct
* @param name
* @param tel
* @param email
*/
public static void insertContact(Context ct, String name, String tel, String email) {
ContentValues values = new ContentValues();
// 首先向RawContacts.CONTENT_URI执行一个空值插入,目的是获取系统返回的rawContactId
Uri rawContactUri = ct.getContentResolver().insert(RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
// 往data表入姓名数据
if (!TextUtils.isEmpty(tel)) {
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);// 内容类型
values.put(StructuredName.GIVEN_NAME, name);
ct.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
}
// 往data表入电话数据
if (!TextUtils.isEmpty(tel)) {
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);// 内容类型
values.put(Phone.NUMBER, tel);
values.put(Phone.TYPE, Phone.TYPE_MOBILE);
ct.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
}
// 往data表入Email数据
if (!TextUtils.isEmpty(email)) {
values.clear();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);// 内容类型
values.put(Email.DATA, email);
values.put(Email.TYPE, Email.TYPE_WORK);
ct.getContentResolver().insert(android.provider.ContactsContract.Data.CONTENT_URI, values);
}
}
private void fillFromCursor() {
namePrefix = getString(StructuredName.PREFIX);
givenName = getString(StructuredName.GIVEN_NAME);
middleName = getString(StructuredName.MIDDLE_NAME);
familyName = getString(StructuredName.FAMILY_NAME);
nameSuffix = getString(StructuredName.SUFFIX);
phoneticGivenName = getString(StructuredName.PHONETIC_GIVEN_NAME);
phoneticMiddleName = getString(StructuredName.PHONETIC_MIDDLE_NAME);
phoneticFamilyName = getString(StructuredName.PHONETIC_FAMILY_NAME);
}
public void addCreationOp(ArrayList<ContentProviderOperation> ops) {
ContentProviderOperation.Builder op = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
.withValue(StructuredName.GIVEN_NAME, givenName)
.withValue(StructuredName.MIDDLE_NAME, middleName)
.withValue(StructuredName.FAMILY_NAME, familyName)
.withValue(StructuredName.PREFIX, namePrefix)
.withValue(StructuredName.SUFFIX, nameSuffix)
.withValue(StructuredName.PHONETIC_GIVEN_NAME, phoneticGivenName)
.withValue(StructuredName.PHONETIC_FAMILY_NAME, phoneticFamilyName)
.withValue(StructuredName.PHONETIC_MIDDLE_NAME, phoneticMiddleName);
ops.add(op.build());
}
public static void insertContact(Context context, String name, String phone) {
// 首先插入空值,再得到rawContactsId ,用于下面插值
ContentValues values = new ContentValues();
// insert a null value
Uri rawContactUri = context.getContentResolver().insert(
RawContacts.CONTENT_URI, values);
long rawContactsId = ContentUris.parseId(rawContactUri);
// 往刚才的空记录中插入姓名
values.clear();
// A reference to the _ID that this data belongs to
values.put(StructuredName.RAW_CONTACT_ID, rawContactsId);
// "CONTENT_ITEM_TYPE" MIME type used when storing this in data table
values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
// The name that should be used to display the contact.
values.put(StructuredName.DISPLAY_NAME, name);
// insert the real values
context.getContentResolver().insert(Data.CONTENT_URI, values);
// 插入电话
values.clear();
values.put(Phone.RAW_CONTACT_ID, rawContactsId);
// String "Data.MIMETYPE":The MIME type of the item represented by this
// row
// String "CONTENT_ITEM_TYPE": MIME type used when storing this in data
// table.
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, phone);
context.getContentResolver().insert(Data.CONTENT_URI, values);
}
/**
* Factory method that creates a ContentValues containing the data
* associated with a RawContact.
*/
private ContentValues makeRawContactData(String displayName,
Uri rawContactUri) {
ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID,
ContentUris.parseId(rawContactUri));
values.put(Data.MIMETYPE,
StructuredName.CONTENT_ITEM_TYPE);
values.put(StructuredName.DISPLAY_NAME,
displayName);
return values;
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args)
{
String[] mSelectionArgs = { mLookupKey, StructuredName.CONTENT_ITEM_TYPE };
return new CursorLoader(getActivity(), ContactsContract.Data.CONTENT_URI, PROJECTION, SELECTION, mSelectionArgs, null);
}