Pro Android学习笔记(一六七):联系人API(10):在Personal Profile中添加raw contact

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

在Personal Profile中添加raw contact和普通的contact差不多,只是数据存放在profile.db中相应的表格中。小例子将在profile中添加一个raw contact。

相关的代码片段如下,和普通联系人相比较,关键是添加raw contact所对应的URI不同。

private void addProfileContact(){ 
    showInfo("1. add an new raw contact."); 
    long rawContactId = insertProfileRawContact(); 
    showInfo("2. add data(Nick name) to the new raw contact"); 
    insertProfileNickName(rawContactId, "flowingflying_" + rawContactId); 
    showInfo("3. add data(PhoneNumber) to the new raw contact"); 
    insertProrilePhone(rawContactId,"13312345678"); 
}

//【1】添加一个新的raw contact。添加在profile.db的raw_contacts表格中
private long insertProfileRawContact(){ 
    ContentValues cv = new ContentValues(); 
    cv.put(RawContacts.ACCOUNT_TYPE, "com.google"); 
    cv.put(RawContacts.ACCOUNT_NAME, "flowingflying@google.com"); 
    ContentResolver cr = mContext.getContentResolver(); 
    Uri uri = cr.insert(ContactsContract.Profile.CONTENT_RAW_CONTACTS_URI, cv); 
    showInfo("insert an new raw contact in personal profile, uri=" + uri.toString()); 
    long rawContactId = ContentUris.parseId(uri); 
    showInfo("new raw contact id = " + rawContactId); 
    return rawContactId; 
}

//【2】在data中为该raw contact添加信息:昵称。我们注意到这里使用的URI和普通的联系人没有区别,系统会通过raw contact id来识别。
private void insertProfileNickName(long rawContactId,String name){ 
    ContentValues cv = new ContentValues(); 
    cv.put(Data.RAW_CONTACT_ID, rawContactId); 
    cv.put(Data.MIMETYPE, CommonDataKinds.Nickname.CONTENT_ITEM_TYPE); 
    cv.put(CommonDataKinds.Nickname.NAME,name); 
    mContext.getContentResolver().insert(Data.CONTENT_URI, cv);        
}

//【3】在data中为该raw contact添加信息:手机号码
private void insertProrilePhone(long rawContactId,String phone){ 
    ContentValues cv = new ContentValues(); 
    cv.put(Data.RAW_CONTACT_ID, rawContactId); 
    cv.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); 
    cv.put(Phone.NUMBER,phone); 
    cv.put(Phone.TYPE, Phone.TYPE_MOBILE); 
    mContext.getContentResolver().insert(Data.CONTENT_URI, cv);            
}

小例子下载

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值