文章转载只能用于非商业性质,且不能带有虚拟货币、积分、注册等附加条件。转载须注明出处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开发相关文章