android 存储联系人(save contact) 总结
终于android 上存储联系人搞定了。由于2.0后存储联系人做了改动,所以存储方法是不一样的,现在贴出来:
(一)2.0以下
public class AddContactAPI3 {
public void addContact(Context context, String name, String organisation,
String phone, String fax, String email, String address,
String website, Bitmap logo) {
ContentValues personValues = new ContentValues();
personValues.put(Contacts.People.NAME, name);
personValues.put(Contacts.People.STARRED, 1);
Uri newPersonUri = context.getContentResolver().insert(
Contacts.People.CONTENT_URI, personValues);
if (newPersonUri != null) {
// add group
long personId = ContentUris.parseId(newPersonUri);
ContentValues group = new ContentValues();
group.put(GroupMembership.PERSON_ID, personId);
group.put(GroupMembership.GROUP_ID, 1);
Uri groupUpdate = context.getContentResolver().insert(
GroupMembership.CONTENT_URI, group);
// add company (organisation)
if (!AppUtils.isBlank(organisation)) {
ContentValues organisationValues = new ContentValues();
Uri orgUri = Uri.withAppendedPath(newPersonUri,
Contacts.Organizations.CONTENT_DIRECTORY);
organisationValues
.put(Contacts.Organizations.COMPANY, organisation);
organisationValues.put(Contacts.Organizations.TYPE,
Contacts.Organizations.TYPE_WORK);
Uri orgUpdate = context.getContentResolver().insert(orgUri,
organisationValues);
}
// add mobile phone number
if (!AppUtils.isBlank(phone)) {
ContentValues mobileValues = new ContentValues();
Uri mobileUri = Uri.withAppendedPath(newPersonUri,
Contacts.People.Phones.CONTENT_DIRECTORY);
mobileValues.put(Contacts.Phones.NUMBER, phone);
mobileValues.put(Contacts.Phones.TYPE, Contacts.Phones.TYPE_MOBILE);
Uri phoneUpdate = context.getContentResolver().insert(mobileUri,
mobileValues);
}
// add fax number
if (!AppUtils.isBlank(fax)) {
ContentValues faxValues = new ContentValues();
Uri faxUri = Uri.withAppendedPath(newPersonUri,
Contacts.People.Phones.CONTENT_DIRECTORY);
faxValues.put(Contacts.Phones.NUMBER, fax);
faxValues.put(Contacts.Phones.TYPE, Contacts.Phones.TYPE_FAX_WORK);
Uri phoneUpdate = context.g
(一)2.0以下
public class AddContactAPI3 {
public void addContact(Context context, String name, String organisation,
String phone, String fax, String email, String address,
String website, Bitmap logo) {
ContentValues personValues = new ContentValues();
personValues.put(Contacts.People.NAME, name);
personValues.put(Contacts.People.STARRED, 1);
Uri newPersonUri = context.getContentResolver().insert(
Contacts.People.CONTENT_URI, personValues);
if (newPersonUri != null) {
// add group
long personId = ContentUris.parseId(newPersonUri);
ContentValues group = new ContentValues();
group.put(GroupMembership.PERSON_ID, personId);
group.put(GroupMembership.GROUP_ID, 1);
Uri groupUpdate = context.getContentResolver().insert(
GroupMembership.CONTENT_URI, group);
// add company (organisation)
if (!AppUtils.isBlank(organisation)) {
ContentValues organisationValues = new ContentValues();
Uri orgUri = Uri.withAppendedPath(newPersonUri,
Contacts.Organizations.CONTENT_DIRECTORY);
organisationValues
.put(Contacts.Organizations.COMPANY, organisation);
organisationValues.put(Contacts.Organizations.TYPE,
Contacts.Organizations.TYPE_WORK);
Uri orgUpdate = context.getContentResolver().insert(orgUri,
organisationValues);
}
// add mobile phone number
if (!AppUtils.isBlank(phone)) {
ContentValues mobileValues = new ContentValues();
Uri mobileUri = Uri.withAppendedPath(newPersonUri,
Contacts.People.Phones.CONTENT_DIRECTORY);
mobileValues.put(Contacts.Phones.NUMBER, phone);
mobileValues.put(Contacts.Phones.TYPE, Contacts.Phones.TYPE_MOBILE);
Uri phoneUpdate = context.getContentResolver().insert(mobileUri,
mobileValues);
}
// add fax number
if (!AppUtils.isBlank(fax)) {
ContentValues faxValues = new ContentValues();
Uri faxUri = Uri.withAppendedPath(newPersonUri,
Contacts.People.Phones.CONTENT_DIRECTORY);
faxValues.put(Contacts.Phones.NUMBER, fax);
faxValues.put(Contacts.Phones.TYPE, Contacts.Phones.TYPE_FAX_WORK);
Uri phoneUpdate = context.g