1.修改联系人
public void update(View view){
String name=this.editText_name.getText().toString().trim();
if(TextUtils.isEmpty(name)){
this.editText_name.setError("联系人的姓名必须输入!");
this.editText_name.requestFocus();
return;
}
//修改raw_contact表中的姓名
ContentValues values=new ContentValues();
values.put("display_name",name);
int rowCount_contact=this.contentResolver.update(Uri.parse(uri_contacts),values,"_id=?",new String[]{id});
values.clear();
//修改data表中data1中的姓名
values.put("data1", name);
int rowCount_data= this.contentResolver.update(Uri.parse(uri_contacts_data),
values,
"raw_contact_id=? and mimetype_id=?",
new String[]{id,"7"});
if(rowCount_contact>0 && rowCount_data>0) {
Toast.makeText(this, "联系人修改成功", Toast.LENGTH_SHORT).show();
finish();
}else {
Toast.makeText(this, "联系人修改失败", Toast.LENGTH_SHORT).show();
}
}
2.增加联系人:
private void insert() {
ContentValues values=new ContentValues();
// 往raw_contacts表中插入一条空数据,目的是获取联系人的id
Uri new_uri=contentResolver.insert(Uri.parse(uri_contacts), values);
//最新插入联系人的id
long id=ContentUris.parseId(new_uri);
// 往data表中插入联系人姓名的数据
values.put("raw_contact_id", id);
values.put("mimetype", "vnd.android.cursor.item/name");
values.put("data1", "小倩");
values.put("data2", "小倩");
contentResolver.insert(Uri.parse(uri_contacts_data), values);
// 往data表中插入联系人的电话信息
values.clear();
values.put("raw_contact_id", id);
values.put("mimetype", "vnd.android.cursor.item/phone_v2");
values.put("data1", "13512011014");
values.put("data2", 2);
contentResolver.insert(Uri.parse(uri_contacts_data), values);
// 往data表中插入联系人的email
values.clear();
values.put("raw_contact_id", id);
values.put("mimetype", "vnd.android.cursor.item/email_v2");
values.put("data1", "xiaoqin@sohu.com");
values.put("data2", 1);
contentResolver.insert(Uri.parse(uri_contacts_data), values);
this.data=this.getData();
adapter.notifyDataSetChanged();
}
}
3.删除联系人
private boolean deleteContactsByDisplayName(String display_name) {
int count=this.contentResolver.delete(Uri.parse(uri_contacts), "display_name=?", new String[]{display_name});
return count>0;
}
4.查询联系人
private List<Map<String,Object>> getData() {
data.clear();
Cursor cursor_raw_contacts=this.contentResolver.query(Uri.parse(uri_contacts), new String[]{"_id","display_name"}, null, null, "_id DESC");
while(cursor_raw_contacts.moveToNext()){
Map<String,Object> map=new HashMap<String,Object>();
int _id=cursor_raw_contacts.getInt(cursor_raw_contacts.getColumnIndex("_id"));
String display_name=cursor_raw_contacts.getString(cursor_raw_contacts.getColumnIndex("display_name"));
map.put("id",_id);
map.put("display_name",display_name);
System.out.println("raw_contact=====_id="+_id+",display_name="+display_name);
//如下代码取电话号码
//select raw_contact_id,data1 from data d,raw_contacts rc where raw_contact_id=rc._id and mimetype_id=5;
Cursor cursor_phone=this.contentResolver.query(Uri.parse(uri_contacts_phones), new String[]{"raw_contact_id", "data1"}, "raw_contact_id=?", new String[]{String.valueOf(_id)}, null);
StringBuilder sb_phones=new StringBuilder();
while(cursor_phone.moveToNext()){
String phone=cursor_phone.getString(cursor_phone.getColumnIndex("data1"));
sb_phones.append(phone+" | ");
}
if(sb_phones.length()>0){
sb_phones.delete(sb_phones.length()-3, sb_phones.length());
}
map.put("phones",sb_phones.toString());
//如下代码取email
//select raw_contact_id,data1 from data d,raw_contacts rc where d.raw_contact_id=rc._id and mimetype_id=1;
Cursor cursor_email=this.contentResolver.query(Uri.parse(uri_contacts_emails), new String[]{"raw_contact_id", "data1"}, "raw_contact_id=?", new String[]{String.valueOf(_id)}, null);
StringBuilder sb_emails=new StringBuilder();
while(cursor_email.moveToNext()){
String email=cursor_email.getString(cursor_email.getColumnIndex("data1"));
sb_emails.append(email+" | ");
}
if(sb_emails.length()>0){
sb_emails.delete(sb_emails.length()-3, sb_emails.length());
}
map.put("emails",sb_emails.toString());
data.add(map);
}
return this.data;
}
必须添加权限:
<!-- 注册读取手机联系人的权限 -->
<uses-permission android:name="android.permission.READ_CONTACTS" />
<!-- 注册向联系人中写入(插入,修改和删除)数据的权限 -->
<uses-permission android:name="android.permission.WRITE_CONTACTS" />