android对通讯录的增删改查

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" />

  

转载于:https://www.cnblogs.com/qcgAd/p/5102686.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值