Pro Android学习笔记(一六四):联系人API(7):添加联系人
2015-04-30 14:34
281 查看
文章转载只能用于非商业性质,且不能带有虚拟货币、积分、注册等附加条件。转载须注明出处http://blog.csdn.net/flowingflying/以及作者@恺风Wei。
添加新的联系人,就要增添raw contacts,需要android.permission.WRITE_CONTACTS的权限。下面是代码片段:
下面是小例子的运行截图,以及运行后在系统中查看联系人的结果:
小例子下载。
相关链接:我的Android开发相关文章
添加新的联系人,就要增添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开发相关文章
相关文章推荐
- Pro Android学习笔记(一六七):联系人API(10):在Personal Profile中添加raw contact
- Pro Android学习笔记(一五九):联系人API(2):联系人应用
- Pro Android学习笔记(一六一):联系人API(4):读取整合联系人
- Pro Android学习笔记(一六二):联系人API(5):读Raw Contacts
- Pro Android学习笔记(一六三):联系人API(6):联系人详细信息
- Pro Android学习笔记(一五八):联系人API(1):账号
- Pro Android学习笔记(一六十):联系人API(3):联系人数据
- Pro Android学习笔记(一六五):联系人API(8):控制整合
- Pro Android学习笔记(一六六):联系人API(9):Personal Profile
- Pro Android学习笔记(一二二):Telephony API(4):发Email
- Pro Android学习笔记(一二三):Telephony API(5):Phone
- Pro Android学习笔记(一二五):Telephony API(7):SIP Phone(下)
- Android学习笔记:访问和添加通讯录中的联系人和联系人数据库表简介一
- Android学习笔记---22_访问通信录中的联系人和添加联系人,使用事物添加联系人...
- Pro Android学习笔记(一二一):Telephony API(3):读短信
- Pro Android学习笔记(一一九):Telephony API(1):发送短信
- Pro Android学习笔记(一二十):Telephony API(2):接收短信
- Pro Android学习笔记(一二四):Telephony API(6):SIP Phone(上)
- Android 向联系人名单新加联系人(添加名称和电话号码两项)
- Pro Android学习笔记(一一十):2D动画(5):Camera