|
private void WriteSingleContact(Contact contact)
{
//cr 是ContentResolver
// DELETE CONTACT
Uri personUri = ContactsContract.RawContacts.CONTENT_URI;
String where =ContactsContract.Contacts.DISPLAY_NAME+"=?";
cr.delete(personUri,where,new String[] {contact.getDisplayName()});
//上面的几行代码删除某个指定联系人
//向contact 中添加联系人姓名,获得一个Id(rawContactId)作为后面添加信息的依据。
ContentValues values = new ContentValues();
values.put(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, contact
.getDisplayName());
Uri rawContactUri = cr.insert(RawContacts.CONTENT_URI, values);
long rawContactId = ContentUris.parseId(rawContactUri);
//也可以只添加一个空ContentValues ,来获得rawContactId。
// ContentValues values = new ContentValues();
//首先向RawContacts.CONTENT_URI执行一个空值插入(raw_contacts 表), 为了获取生成的联系人 ID
Uri rawContactUri = this.getContext().getContentResolver().insert(RawContacts.CONTENT_URI, values);
//?
//? //然后获取系统返回的rawContactId , 就是新加入的这个联系人的 ID
//? // long rawContactId = ContentUris.parseId(rawContactUri);
//添加联系人姓名
values.clear();
values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
values.put(ContactsContract.Data.MIMETYPE,
StructuredName.CONTENT_ITEM_TYPE);
values.put(StructuredName.DISPLAY_NAME, contact.getDisplayName());
cr.insert(ContactsContract.Data.CONTENT_URI, values);
Uri phoneUri = null;
Uri emailUri = null;
Uri addressUri = null;
Uri imUri = null;
Uri orgUri = null;
Uri noteUri = null;
//添加电话号码
for (Phone aPhone : contact.getPhone()) {
phoneUri = Uri.withAppendedPath(rawContactUri,
ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
values.clear();
values.put(ContactsContract.CommonDataKinds.Phone.TYPE, aPhone
.getType());
values.put(ContactsContract.CommonDataKinds.Phone.IS_SUPER_PRIMARY,
1);
values.put(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, aPhone
.getNumber());
cr.insert(phoneUri, values);
}
//添加Email
for (Email aEmail : contact.getEmail()) {
emailUri = Uri.withAppendedPath(rawContactUri,
ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
values.clear();
values.put(ContactsContract.CommonDataKinds.Email.TYPE, aEmail
.getType());
values.put(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Email.DATA, aEmail
.getAddress());
cr.insert(emailUri, values);
}
for (String aNote : contact.getNotes()) {
noteUri = Uri.withAppendedPath(rawContactUri,
ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
values.clear();
values.put(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Note.NOTE, aNote);
cr.insert(noteUri, values);
}
//添加IM
for (IM aIM : contact.getImAddresses()) {
imUri = Uri.withAppendedPath(rawContactUri,
ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
values.clear();
values.put(ContactsContract.CommonDataKinds.Im.PROTOCOL, aIM
.getType());
values.put(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Im.DATA, aIM.getName());
cr.insert(imUri, values);
}
添加Organization
for (Organization aORG : contact.getOrganization()) {
orgUri = Uri.withAppendedPath(rawContactUri,
ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
values.clear();
values.put(ContactsContract.CommonDataKinds.Organization.TYPE, aORG
.getType());
values.put(ContactsContract.CommonDataKinds.Organization.TITLE,
aORG.getTitle());
values
.put(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
values.put(ContactsContract.CommonDataKinds.Organization.DATA, aORG
.getOrganization());
cr.insert(orgUri, values);
}
//添加Address,(国家,城市,街道,邮政编码等)
for (Address aAdd : contact.getAddresses()) {
addressUri = Uri.withAppendedPath(rawContactUri,
ContactsContract.Contacts.Data.CONTENT_DIRECTORY);
values.clear();
values
.put(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);
if (aAdd.getCity() != null)
values.put(
ContactsContract.CommonDataKinds.StructuredPostal.CITY,
aAdd.getCity());
if (aAdd.getCountry() != null)
values
.put(
ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY,
aAdd.getCountry());
if (aAdd.getPoBox() != null)
values
.put(
ContactsContract.CommonDataKinds.StructuredPostal.POBOX,
aAdd.getPoBox());
if (aAdd.getPostalCode() != null)
values
.put(
ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE,
aAdd.getPostalCode());
if (aAdd.getStreet() != null)
values
.put(
ContactsContract.CommonDataKinds.StructuredPostal.STREET,
aAdd.getStreet());
if (aAdd.getState() != null)
values
.put(
ContactsContract.CommonDataKinds.StructuredPostal.REGION,
aAdd.getState());
values.put(ContactsContract.CommonDataKinds.StructuredPostal.TYPE,
aAdd.getType());
cr.insert(addressUri, values);
}
}
public boolean WriteContact(ArrayList<Contact> LstContact) {
try {
for (Contact contact : LstContact) {
WriteSingleContact(contact);
}
return true;
} catch (Exception ex) {
Log.d("ERROR", ex.toString());
return false;
}
}
} |
|