Pro Android学习笔记(一六四):联系人API(7):添加联系人

文章转载只能用于非商业性质,且不能带有虚拟货币、积分、注册等附加条件。转载须注明出处http://blog.csdn.net/flowingflying/以及作者@恺风Wei

添加新的联系人,就要增添raw contacts,需要android.permission.WRITE_CONTACTS的权限。下面是代码片段:

	//添加联系人的代码片段,入口为addContactTest()。showInfo()为在activity框中显示信息或其他显示信息的方式
	private void addContactTest(String name, String phoneNumber){
		showInfo("【1】insert a raw contact into table raw_contacts"); 
		long rawContactId = insertRawContact();
		showInfo("\n【2】insert the name("+name +") in table data");
		insertName(rawContactId,name);
		showInfo("\n【3】insert the phone number(" + phoneNumber+ ") in table data");
		insertPhone(rawContactId,phoneNumber);
		showInfo("\n【4】Now, look at the raw contact info");
		showRawContactInfo(rawContactId);
	}
	
	/*【1】在table raw_contacts表中增添entry,使用的uri为RawContacts.CONTENT_URI,指定所属的账号
	 * ,即account type和account name。*/
	private long insertRawContact(){
		ContentValues cv = new ContentValues();
		cv.put(RawContacts.ACCOUNT_TYPE,"cn.wei"); 
		cv.put(RawContacts.ACCOUNT_NAME, "flowingflying@wei.cn"); 
		
		ContentResolver cr = mContext.getContentResolver();
		Uri rawContactUri = cr.insert(RawContacts.CONTENT_URI, cv);
		showInfo("raw contact uri = " + rawContactUri.toString());
		return ContentUris.parseId(rawContactUri);
	}
	
	/*【2】raw contacts的具体信息是存放在table data中的,下面代码在data表中添加该raw contact的具体信息: displayname
	 * table data使用的uri为Data.CONTENT_URI */
	private void insertName(long rawContactId, String name){
		ContentValues cv = new ContentValues();
		cv.put(Data.RAW_CONTACT_ID,rawContactId);
		cv.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);//vnd.android.cursor.item/name
		cv.put(StructuredName.DISPLAY_NAME, name); //对应data1
		Uri uri = mContext.getContentResolver().insert(Data.CONTENT_URI, cv);
		showInfo("uri=" + (uri == null ? "null" : uri.toString()));
	}
	
	/*【3】在data表中添加该raw contact的具体信息: phone */
	private void insertPhone(long rawContactId, String phoneNumber){
		ContentValues cv = new ContentValues();
		cv.put(Data.RAW_CONTACT_ID,rawContactId);
		cv.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);//vnd.android.cursor.item/phone_v2
		cv.put(Phone.NUMBER, phoneNumber); //data2
		cv.put(Phone.TYPE,Phone.TYPE_HOME); //data1
		Uri uri = mContext.getContentResolver().insert(Data.CONTENT_URI, cv);
		showInfo("uri=" + (uri == null ? "null" : uri.toString()));
	}
	
	/*【4】显示该raw contact的详细信息,通过raw contract id进行索引,getACursor()以及ContactInfo见前一学习笔记的小例子*/
	private void showRawContactInfo(long rawContactId){
		Cursor c = null;
		try{
			c = getACurosor(ContactsContract.RawContactsEntity.CONTENT_URI.toString(),"_id=" + rawContactId);
			for(c.moveToFirst(); !c.isAfterLast() ; c.moveToNext()){
				ContactInfo one = new ContactInfo();
				one.fillinFrom(c);
				showInfo("【】" + one.toString());
			}
		}finally{
			if(c != null)
				c.close();
		}
	}

下面是小例子的运行截图,以及运行后在系统中查看联系人的结果:

  

小例子下载

相关链接:我的Android开发相关文章

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值