当前位置: 代码迷 >> Android >> Android 剔除手机联系人,添加手机联系人,更新手机联系人信息
  详细解决方案

Android 剔除手机联系人,添加手机联系人,更新手机联系人信息

热度:87   发布时间:2016-05-01 20:37:36.0
Android 删除手机联系人,添加手机联系人,更新手机联系人信息
添加联系人


private void addContact(Context context, String name,
String organisation,String phone, String fax, String email, String address,String website){

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

//在名片表插入一个新名片
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null).withValue(ContactsContract.RawContacts._ID, 0).withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null).withValue(
ContactsContract.RawContacts.AGGREGATION_MODE,ContactsContract.RawContacts.AGGREGATION_MODE_DISABLED).build());

// add name
//添加一条新名字记录;对应RAW_CONTACT_ID为0的名片
if (!name.equals("")) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(
ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE).withValue(
ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,name).build());
}

//添加昵称
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(
ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.Nickname.NAME,"Anson昵称").build());

// add company
if (!organisation.equals("")) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE).withValue(
ContactsContract.CommonDataKinds.Organization.COMPANY,organisation).withValue(
ContactsContract.CommonDataKinds.Organization.TYPE,ContactsContract.CommonDataKinds.Organization.TYPE_WORK).build());
}

// add phone
if (!phone.equals("")) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,phone).withValue(ContactsContract.CommonDataKinds.Phone.TYPE,1).build());
}

// add Fax
if (!fax.equals("")) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE).withValue(
ContactsContract.CommonDataKinds.Phone.NUMBER,fax)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK).build());
}



// add email
if (!email.equals("")) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(
ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE).withValue(ContactsContract.CommonDataKinds.Email.DATA,email).withValue(ContactsContract.CommonDataKinds.Email.TYPE,1).build());
}

// add address
if (!address.equals("")) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(
ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE).withValue(
ContactsContract.CommonDataKinds.StructuredPostal.STREET,address)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE,
ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK).build());
}

// add website
if (!website.equals("")) {
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE).withValue(
ContactsContract.CommonDataKinds.Website.URL,website)
.withValue(
ContactsContract.CommonDataKinds.Website.TYPE,
ContactsContract.CommonDataKinds.Website.TYPE_WORK).build());
}

// add IM
String qq1="452824089";
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI).withValueBackReference(
ContactsContract.Data.RAW_CONTACT_ID, 0).withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE).withValue(
ContactsContract.CommonDataKinds.Im.DATA1,qq1)
.withValue(
ContactsContract.CommonDataKinds.Im.PROTOCOL,
ContactsContract.CommonDataKinds.Im.PROTOCOL_QQ).build());

// add logo image
// Bitmap bm = logo;
// if (bm != null) {
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
// bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
// byte[] photo = baos.toByteArray();
// if (photo != null) {
//
// ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
// .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
// .withValue(ContactsContract.Data.MIMETYPE,
// ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
// .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, photo)
// .build());
// }
// }

try {
context.getContentResolver().applyBatch(
ContactsContract.AUTHORITY, ops);
} catch (Exception e){
}

}


1.删除联系人
private void delContact(Context context, String name) {

Cursor cursor = getContentResolver().query(Data.CONTENT_URI,new String[] { Data.RAW_CONTACT_ID },

ContactsContract.Contacts.DISPLAY_NAME + "=?",new String[] { name }, null);

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

if (cursor.moveToFirst()) {
do {
long Id = cursor.getLong(cursor.getColumnIndex(Data.RAW_CONTACT_ID));

ops.add(ContentProviderOperation.newDelete(
ContentUris.withAppendedId(RawContacts.CONTENT_URI,Id)).build());
try {
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
}
catch (Exception e){}
} while (cursor.moveToNext());
cursor.close();
}
}


更新联系人信息

private void updateContact(Context context,String oldname, String name, String phone, String email,String website, String organization, String note) {

Cursor cursor = getContentResolver().query(Data.CONTENT_URI,new String[] { Data.RAW_CONTACT_ID },

ContactsContract.Contacts.DISPLAY_NAME + "=?",new String[] { oldname }, null);
cursor.moveToFirst();
String id = cursor.getString(cursor.getColumnIndex(Data.RAW_CONTACT_ID));
cursor.close();
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();


更新电话号码

ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(

Data.RAW_CONTACT_ID + "=?" + " AND "+ ContactsContract.Data.MIMETYPE + " = ?" +
" AND " + Phone.TYPE + "=?",new String[] { String.valueOf(id),Phone.CONTENT_ITEM_TYPE,
String.valueOf(Phone.TYPE_HOME) }).withValue(Phone.NUMBER, phone).build());

// 更新email
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(Data.RAW_CONTACT_ID + "=?" + " AND "+ ContactsContract.Data.MIMETYPE + " = ?" +" AND " + Email.TYPE + "=?",new String[] { String.valueOf(id),Email.CONTENT_ITEM_TYPE,
String.valueOf(Email.TYPE_HOME) }).withValue(Email.DATA, email).build());

// 更新姓名
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(Data.RAW_CONTACT_ID + "=?" + " AND "+ ContactsContract.Data.MIMETYPE + " = ?",new String[] { String.valueOf(id),StructuredName.CONTENT_ITEM_TYPE }).withValue(StructuredName.DISPLAY_NAME, name).build());

// 更新网站
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(Data.RAW_CONTACT_ID + "=?" + " AND "+ ContactsContract.Data.MIMETYPE + " = ?",new String[] { String.valueOf(id),Website.CONTENT_ITEM_TYPE }).withValue(Website.URL, website).build());

// 更新公司
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(Data.RAW_CONTACT_ID + "=?" + " AND "+ ContactsContract.Data.MIMETYPE + " = ?",new String[] { String.valueOf(id),Organization.CONTENT_ITEM_TYPE })
.withValue(Organization.COMPANY, organization).build());

// 更新note
ops.add(ContentProviderOperation.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(Data.RAW_CONTACT_ID + "=?" + " AND "+ ContactsContract.Data.MIMETYPE + " = ?",new String[] { String.valueOf(id),Note.CONTENT_ITEM_TYPE }).withValue(Note.NOTE, note).build());

try{
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
}
}


添加权限
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
<uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>

///联系人分组操作
Cursor cursor = getContentResolver().query(Groups.CONTENT_URI,null,null,null,null); 
for (cursorAll.moveToFirst();!(cursorAll.isAfterLast());cursorAll.moveToNext()) { 
组名:cursor.getString(cursor.getColumnIndex(Groups.TITLE)); 
组id:curso.getLong(cursor.getColumnIndex(Groups._ID)); 

 
新建组(名字为name): 
    ContentValues values = new ContentValues(); 
    String name = "同学";
   values.put(Groups.TITLE, name); 
    getContentResolver().insert(Groups.CONTENT_URI, values); 
 
删除组(Id为groupId): 
    getContentResolver().delete(Uri.parse(Groups.CONTENT_URI +"?" +ContactsContract.CALLER_IS_SYNCADAPTER + "=true"),Groups._ID+"="+groupId,null); 
 
给组重命名(oldName;newName;groupId): 
Uri uri = ContentUris.withAppendedId(Groups.CONTENT_URI, groupId); 
ContentValues values = new ContentValues(); 
values.put(Groups.TITLE,newName); 
getContentResolver().update(uri,values,null,null); 
 
给组添加成员(groupId,personId): 
ContentValues values = new ContentValues();       
values.put(ContactsContract.CommonDataKinds.GroupMembership.RAW_CONTACT_ID,personId);       
values.put(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID,groupId);       
values.put(ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE,ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE);         
getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values); 
 
给组移除成员(groupId,personId): 
getContentResolver().delete(ContactsContract.Data.CONTENT_URI,ContactsContract.CommonDataKinds.GroupMembership.RAW_CONTACT_ID + "=? and " +ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID + "=? and " +ContactsContract.CommonDataKinds.GroupMembership.MIMETYPE + "=?",new String[]{"" + personId,"" + groupId,ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE}); 

注:
1 添加成员到分组的personId是指RawContacts._ID,而不是Contacts._ID。
2 当一个联系人名片里有几个联系人的情况时,要把每个RawContacts._ID都添加进分组。

这个问题很严重啊,使用Contacts._ID,碰到有合并的联系人以后,关于分组的所有操作都会错乱掉。
  相关解决方案