Android管理联系人(包含添加,查询,修改和删除;以及不同版本区别)
通过Android系统提供的接口,可以很方便的管理联系人信息。
一、添加
1、添加联系人
1.6上的代码:
String peopleName = "name";ContentValues personValues = new ContentValues();// namepersonValues.put(Contacts.People.NAME, peopleName);/* STARRED 0 = Contacts, 1 = Favorites */personValues.put(Contacts.People.STARRED, 0);Uri newPersonUri = Contacts.People.createPersonInMyContactsGroup(getContentResolver(), personValues);
2.1时需要用下面的代码才可以添加:
String peopleName = "name";ContentValues personValues = new ContentValues();// namepersonValues.put(Contacts.People.NAME, peopleName);newPersonUri = getContentResolver().insert(People.CONTENT_URI, personValues);
2、添加电话号码
根据TYPE的不同,可以添加不同类型的电话号码。
1.6上支持的TYPE有:TYPE_CUSTOM,TYPE_FAX_HOME,TYPE_FAX_WORK,TYPE_HOME,TYPE_MOBILE,TYPE_OTHER,TYPE_PAGER,
TYPE_WO
Uri uri = null;ContentValues values = new ContentValues();// add phone numberString tel = "86-21-65432100";if (!AppUtils.isEmpty(tel)) {values.clear();uri = Uri.withAppendedPath(newPersonUri, Contacts.People.Phones.CONTENT_DIRECTORY);values.put(Contacts.Phones.TYPE, Contacts.Phones.TYPE_HOME);values.put(Contacts.Phones.NUMBER, tel);getContentResolver().insert(uri, values);}
3、添加Email地址
通过改变KIND的值,可以添加不同的联系方式。
1.6支持KIND_EMAIL,KIND_IM,KIND_ORGANIZATION,KIND_PHONE,KIND_POSTAL
// add email addressString email = "[email protected]";if (!AppUtils.isEmpty(email)) {values.clear();uri = Uri.withAppendedPath(newPersonUri, Contacts.People.ContactMethods.CONTENT_DIRECTORY);values.put(Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL);values.put(Contacts.ContactMethods.DATA, email);values.put(Contacts.ContactMethods.TYPE, Contacts.ContactMethods.TYPE_WORK);getContentResolver().insert(uri, values);}
4、添加公司和职务
// add company name & titleString company = "Google?";String position= "CEO!!!";if (!AppUtils.isEmpty(company) || !AppUtils.isEmpty(position)) {values.clear();uri = Uri.withAppendedPath(newPersonUri, Contacts.Organizations.CONTENT_DIRECTORY);// company nameif (!AppUtils.isEmpty(company)) {values.put(Contacts.Organizations.COMPANY, companyNameText);}// positionif (!AppUtils.isEmpty(position)) {values.put(Contacts.Organizations.TITLE, positionNameText);}values.put(Contacts.Organizations.TYPE, Contacts.Organizations.TYPE_WORK);getContentResolver().insert(uri, values);}
二、查询
要实现查询功能,可能需要有点SQL的基础。
1、查询人名
// the contents want to getString projection[] = new String[] { People._ID, People.NAME };// the name to be foundString name = "find me";// start searchCursor cur = getContentResolver().query(People.CONTENT_URI, projection, // select sentencePeople.NAME + " = ?", // where sentencenew String[] { name }, // where valuesPeople.NAME); // order byif (cur.moveToFirst()) {// get the resultsdo {String id = cur.getString(cur.getColumnIndex(People._ID));String name = cur.getString(cur.getColumnIndex(People.NAME));} while (cur.moveToNext());}// close while finishif (cur != null) {cur.close();}
通过修改projection的内容,可以取得不同的内容。
2、查询电话号码
如果要获得电话号码,就可以改成(id由上面的代码获得)
String phoneProjection[] = new String[] { Contacts.Phones.PERSON_ID, Contacts.Phones.NUMBER };Cursor phoneCursor = getContentResolver().query(Contacts.Phones.CONTENT_URI, phoneProjection, // selectContacts.Phones.PERSON_ID + " = " + id, // where (another style)null, Contacts.Phones.DEFAULT_SORT_ORDER); // orderif (phoneCursor.moveToFirst()) {// get the resultsdo {String phone = phoneCursor.getString(phoneCursor.getColumnIndex(Contacts.Phones.NUMBER));} while (phoneCursor.moveToNext());}// close while finishif (phoneCursor != null) {phoneCursor.close();}
3、查询email地址
如果要获得email地址,稍微麻烦点
String emailProjection[] = new String[] { Contacts.Phones.PERSON_ID, Contacts.ContactMethods.KIND, Contacts.ContactMethods.DATA };Cursor emailCursor = getContentResolver().query(Contacts.ContactMethods.CONTENT_URI, emailProjection, // selectContacts.ContactMethods.PERSON_ID + " = " + id, // wherenull, Contacts.ContactMethods.DEFAULT_SORT_ORDER); // orderif (emailCursor.moveToFirst()) {do {int kind = emailCursor.getInt(emailCursor.getColumnIndex(Contacts.ContactMethods.KIND));if (Contacts.KIND_EMAIL == kind) {email = emailCursor.getString(emailCursor.getColumnIndex(Contacts.ContactMethods.DATA));}} while (emailCursor.moveToNext());}// close while finishif (emailCursor != null) {emailCursor.close();}
三、修改
四、删除
更多相关文章
- 在NDK中使用STL
- Android中利用OnTouchListener在ImageView中框选显示图片
- ubuntu右键添加打开终端的快捷菜单
- Android(安卓)studio gradle 依赖后报错后出现Failed to resolve
- Android(安卓)Studio添加so库
- Android(安卓)Activity间的过渡动画
- Android(安卓)Studio编译提示警告xxx已过时
- Android(安卓)快速开发之快速实现“我”界面
- Android(安卓)联系人开发- 保存联系人