(转)Android 读取联系人(详细)
- import java.io.InputStream;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import android.content.ContentUris;
- import android.content.Context;
- import android.database.Cursor;
- import android.net.Uri;
- import android.provider.ContactsContract;
- import android.util.Log;
- public class CopyOfContactCollector {
- private static final String TAG = CopyOfContactCollector.class.getSimpleName();
- private static final String KEY_BIRTH = "birthday";
- private static final String KEY_ADDR = "address";
- private static final String KEY_NICKNAME = "nickname";
- private static final String KEY_ORG = "org";
- private static final String KEY_IM = "IM";
- private static final String KEY_NOTE = "note";
- private static final String KEY_EMAIL = "email";
- private static final String KEY_PHONE = "phone";
- private static final String KEY_WEBSITE = "website";
- private static final String KEY_PHOTO = "photo";
- private Context context;
- public CopyOfContactCollector(Context context) {
- this.context = context;
- }
- public void getContacts() {
- Cursor cursor = null;
- try {
- cursor = context.getContentResolver().query(
- ContactsContract.Contacts.CONTENT_URI,
- null,
- null,
- null,
- null);
- JSONArray contactList = new JSONArray();
- while (cursor.moveToNext()) {
- String contactId = cursor.getString(cursor
- .getColumnIndex(ContactsContract.Contacts._ID));
- int hasPhone = cursor.getInt(cursor
- .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
- String contactName = cursor.getString(cursor
- .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
- long photoId = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
- JSONObject item = new JSONObject();
- item.put("id", contactId);
- item.put("name", contactName);
- // phone
- if (hasPhone == 1) {
- this.getPhone(contactId, item);
- }
- // photo
- this.getPhoto(contactId, photoId, item);
- this.getEmail(contactId, item);
- // address
- this.getAddress(contactId, item);
- // birthdat
- this.getBirthday(contactId, item);
- // instant message
- this.getIM(contactId, item);
- // nickname
- this.getNickname(contactId, item);
- // note
- this.getNote(contactId, item);
- // org
- this.getOrg(contactId, item);
- // website
- this.getWebsite(contactId, item);
- contactList.put(item);
- }
- JSONObject data = new JSONObject();
- data.put("CONTACTS", contactList);
- data.put("TIMESTAMP", System.currentTimeMillis());
- System.out.println(data.toString());
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (cursor != null) {
- cursor.close();
- }
- }
- }
- private void getPhone (String contactId, JSONObject data) throws JSONException {
- Cursor pCur = null;
- try {
- pCur = context.getContentResolver().query(
- ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
- null,
- ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
- new String[]{contactId + ""},
- null);
- JSONArray phoneList = new JSONArray();
- while (pCur.moveToNext()) {
- int type = pCur.getInt(pCur.getColumnIndex(
- ContactsContract.CommonDataKinds.Phone.TYPE));
- String phoneType = ContactsContract.CommonDataKinds.Phone.getTypeLabel(
- context.getResources(), type, "").toString();
- String phoneNumber = pCur.getString(pCur.getColumnIndex(
- ContactsContract.CommonDataKinds.Phone.NUMBER));
- JSONObject item = new JSONObject();
- item.put("phone", phoneNumber);
- item.put("type", phoneType);
- phoneList.put(item);
- }
- data.put(KEY_PHONE, phoneList);
- } finally {
- if (pCur != null) {
- pCur.close();
- }
- }
- }
- private void getEmail (String contactId, JSONObject data) throws JSONException {
- Cursor emailCur = null;
- try {
- emailCur = context.getContentResolver().query(
- ContactsContract.CommonDataKinds.Email.CONTENT_URI,
- null,
- ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
- new String[]{contactId},
- null);
- JSONArray emailList = new JSONArray();
- while (emailCur.moveToNext()) {
- String email = emailCur.getString(emailCur.getColumnIndex(
- ContactsContract.CommonDataKinds.Email.DATA));
- int type = emailCur.getInt(emailCur.getColumnIndex(
- ContactsContract.CommonDataKinds.Email.TYPE));
- String emailType = ContactsContract.CommonDataKinds.Email.getTypeLabel(
- context.getResources(), type, "").toString();
- JSONObject item = new JSONObject();
- item.put("email", email);
- item.put("type", emailType);
- emailList.put(item);
- }
- data.put(KEY_EMAIL, emailList);
- } finally {
- if (emailCur != null) {
- emailCur.close();
- }
- }
- }
- private void getNote (String contactId, JSONObject data) throws JSONException {
- Cursor noteCur = null;
- try {
- String noteWhere =
- ContactsContract.Data.CONTACT_ID + " = ? AND " +
- ContactsContract.Data.MIMETYPE + " = ?";
- String[] noteWhereParams = new String[]{
- contactId,
- ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE};
- noteCur = context.getContentResolver().query(
- ContactsContract.Data.CONTENT_URI,
- null,
- noteWhere,
- noteWhereParams,
- null);
- if (noteCur.moveToFirst()) {
- String note = noteCur.getString(noteCur.getColumnIndex(
- ContactsContract.CommonDataKinds.Note.NOTE));
- data.put(KEY_NOTE, note);
- }
- } finally {
- if (noteCur != null) {
- noteCur.close();
- }
- }
- }
- private void getWebsite (String contactId, JSONObject data) throws JSONException {
- Cursor websiteCur = null;
- try {
- String where =
- ContactsContract.Data.CONTACT_ID + " = ? AND " +
- ContactsContract.Data.MIMETYPE + " = ?";
- String[] whereParams = new String[]{
- contactId,
- ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE};
- websiteCur = context.getContentResolver().query(
- ContactsContract.Data.CONTENT_URI,
- null,
- where,
- whereParams,
- null);
- if (websiteCur.moveToFirst()) {
- String website = websiteCur.getString(websiteCur.getColumnIndex(
- ContactsContract.CommonDataKinds.Website.URL));
- data.put(KEY_WEBSITE, website);
- }
- } finally {
- if (websiteCur != null) {
- websiteCur.close();
- }
- }
- }
- private void getIM (String contactId, JSONObject data) throws JSONException {
- Cursor imCur = null;
- try {
- String imWhere =
- ContactsContract.Data.CONTACT_ID + " = ? AND " +
- ContactsContract.Data.MIMETYPE + " = ?";
- String[] imWhereParams = new String[]{contactId,
- ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE};
- imCur = context.getContentResolver().query(
- ContactsContract.Data.CONTENT_URI,
- null,
- imWhere,
- imWhereParams,
- null);
- JSONArray imList = new JSONArray();
- while (imCur.moveToNext()) {
- String imName = imCur.getString(
- imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA));
- int type = imCur.getInt(
- imCur.getColumnIndex(ContactsContract.CommonDataKinds.Im.TYPE));
- String imType = ContactsContract.CommonDataKinds.Im.getTypeLabel(
- context.getResources(), type, "").toString();
- JSONObject item = new JSONObject();
- item.put("imName", imName);
- item.put("imType", imType);
- imList.put(item);
- }
- data.put(KEY_IM, imList);
- } finally {
- if (imCur != null) {
- imCur.close();
- }
- }
- }
- private void getOrg (String contactId, JSONObject data) throws JSONException {
- Cursor orgCur = null;
- try {
- String orgWhere =
- ContactsContract.Data.CONTACT_ID + " = ? AND " +
- ContactsContract.Data.MIMETYPE + " = ?";
- String[] orgWhereParams = new String[]{contactId,
- ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};
- orgCur = context.getContentResolver().query(
- ContactsContract.Data.CONTENT_URI,
- null,
- orgWhere,
- orgWhereParams,
- null);
- JSONArray orgList = new JSONArray();
- while (orgCur.moveToNext()) {
- String orgName = orgCur.getString(orgCur.getColumnIndex(
- ContactsContract.CommonDataKinds.Organization.DATA));
- String title = orgCur.getString(orgCur.getColumnIndex(
- ContactsContract.CommonDataKinds.Organization.TITLE));
- JSONObject item = new JSONObject();
- item.put("orgName", orgName);
- item.put("title", title);
- orgList.put(item);
- }
- data.put(KEY_ORG, orgList);
- } finally {
- if (orgCur != null) {
- orgCur.close();
- }
- }
- }
- private void getNickname (String contactId, JSONObject data) throws JSONException {
- Cursor nicknameCur = null;
- try {
- String nicknameWhere =
- ContactsContract.Data.CONTACT_ID + " = ? AND " +
- ContactsContract.Data.MIMETYPE + " = ?";
- String[] nicknameWhereParams = new String[]{contactId,
- ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE};
- nicknameCur = context.getContentResolver().query(
- ContactsContract.Data.CONTENT_URI,
- null,
- nicknameWhere,
- nicknameWhereParams,
- null);
- while (nicknameCur.moveToNext()) {
- String nickname = nicknameCur.getString(nicknameCur.getColumnIndex(
- ContactsContract.CommonDataKinds.Nickname.NAME));
- data.put(KEY_NICKNAME, nickname);
- break;
- }
- } finally {
- if (nicknameCur != null) {
- nicknameCur.close();
- }
- }
- }
- private void getBirthday (String contactId, JSONObject data) throws JSONException {
- Cursor bCur = null;
- try {
- bCur = context.getContentResolver().query(
- ContactsContract.Data.CONTENT_URI,
- new String[] {ContactsContract.CommonDataKinds.Event.DATA },
- ContactsContract.Data.CONTACT_ID+" = "+contactId+" AND "
- +ContactsContract.Data.MIMETYPE+" = '"
- +ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE+"' AND "
- +ContactsContract.CommonDataKinds.Event.TYPE+" = "
- +ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY,
- null,
- null);
- while (bCur.moveToNext()) {
- String birthday = bCur.getString(0);
- data.put(KEY_BIRTH, birthday);
- break;
- }
- } finally {
- if (bCur != null) {
- bCur.close();
- }
- }
- }
- /**
- * Get address infomation of given contact.
- *
- * @param contactId
- * @param data
- * @throws JSONException
- */
- private void getAddress (String contactId, JSONObject data) throws JSONException {
- Cursor postals = null;
- try {
- // address
- postals = context.getContentResolver().query(
- ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
- null,
- ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = " + contactId,
- null,
- null);
- int postFormattedNdx = postals.getColumnIndex(
- ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS);
- int postTypeNdx = postals.getColumnIndex(
- ContactsContract.CommonDataKinds.StructuredPostal.TYPE);
- int postStreetNdx = postals.getColumnIndex(
- ContactsContract.CommonDataKinds.StructuredPostal.STREET);
- JSONArray addrList = new JSONArray();
- while (postals.moveToNext()) {
- String addressType = ContactsContract.CommonDataKinds.StructuredPostal
- .getTypeLabel(context.getResources(), postTypeNdx, "").toString();
- String str1 = postals.getString(postFormattedNdx);
- String str2 = postals.getString(postStreetNdx);
- JSONObject item = new JSONObject();
- item.put("addressType", addressType);
- item.put("address", str1 + str2);
- addrList.put(item);
- }
- data.put(KEY_ADDR, addrList);
- } finally {
- if (postals != null) {
- postals.close();
- }
- }
- }
- /**
- * Get the photo of given contact.
- *
- * @param cr
- * @param id
- * @param photo_id
- * @return
- */
- private void getPhoto (String contactId, long photoId, JSONObject data) throws JSONException {
- Uri uri = ContentUris.withAppendedId(
- ContactsContract.Contacts.CONTENT_URI, Long.parseLong(contactId));
- InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(
- context.getContentResolver(), uri);
- if (input != null) {
- /*Bitmap photo = BitmapFactory.decodeStream(input);
- data.put(KEY_PHOTO, photo);*/
- } else {
- Log.d(TAG, "First try failed to load photo!");
- }
- byte[] photoBytes = null;
- Uri photoUri = ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, photoId);
- Cursor c = context.getContentResolver().query(
- photoUri,
- new String[] {ContactsContract.CommonDataKinds.Photo.PHOTO},
- null,
- null,
- null);
- try {
- if (c.moveToFirst()) {
- photoBytes = c.getBlob(0);
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- c.close();
- }
- if (photoBytes != null) {
- /*Bitmap photo = BitmapFactory.decodeByteArray(photoBytes, 0, photoBytes.length);
- data.put(KEY_PHOTO, photo);*/
- } else {
- Log.d(TAG, "Second try also failed!");
- }
- }
- }
结果如下:
- {
- "TIMESTAMP": 946692989880,
- "CONTACTS": [
- {
- "id": "801",
- "phone": [
- {
- "type": "手机",
- "phone": "11122"
- },
- {
- "type": "家",
- "phone": "222111"
- }
- ],
- "org": [],
- "IM": [],
- "address": [],
- "email": [
- {
- "type": "工作",
- "email": "zhangsan@qq.com"
- }
- ],
- "name": "john"
- },
- {
- "id": "802",
- "org": [],
- "IM": [],
- "address": [],
- "email": [],
- "name": "jack"
- },
- {
- "id": "803",
- "phone": [
- {
- "type": "手机",
- "phone": "1300070302533"
- }
- ],
- "org": [],
- "IM": [],
- "address": [],
- "email": [],
- "name": "alex"
- },
- {
- "id": "1064",
- "birthday": "2000-01-06",
- "phone": [
- {
- "type": "手机",
- "phone": "18811112222"
- },
- {
- "type": "家",
- "phone": "18811113333"
- },
- {
- "type": "工作",
- "phone": "18811114444"
- }
- ],
- "IM": [
- {
- "imName": "12345678",
- "imType": "其他"
- },
- {
- "imName": "13245678",
- "imType": "其他"
- }
- ],
- "website": "www.baidu.com",
- "nickname": "nickname",
- "address": [
- {
- "address": "百度市谷歌西路10086号",
- "addressType": "自定义"
- }
- ],
- "email": [
- {
- "type": "工作",
- "email": "office@xxx.com"
- },
- {
- "type": "住宅",
- "email": "home@xxx.com"
- }
- ],
- "name": "小强",
- "org": [
- {
- "orgName": "三月",
- "title": "攻城狮"
- }
- ],
- "note": "comment for test"
- }
- ]
- }
(转)Android 读取联系人(详细)的更多相关文章
- android 读取联系人
设置读取权限 <uses-permission android:name="android.permission.READ_CONTACTS" /> <u ...
- Android 读取手机联系人、拨号、发送短信及长按菜单的操作
本示例实现了读取手机联系人,拨号.发送短信及长按出现菜单选项的操作↓ 1.Andrid项目结构图↓主要操作图中红色方框内的文件. 2.首先布局代码如下↓ a, main.xml 程序运行的主界面,主要 ...
- android之读取联系人信息
联系人信息被存放在一个contacts2.db的数据库中 主要的两张表 读取联系人 读取联系人需要知道联系人内容提供者的地址,以及对应的操作对象.一般情况下操作对象是的命名方式和表明是一致的. 布局文 ...
- Android基础笔记(十四)- 内容提供者读取联系人
利用内容提供者读取联系人 利用内容提供者插入联系人 内容观察者的原理 利用内容观察者监听系统应用数据库或者自己应用数据库的变化 利用内容提供者读取联系人 读取联系人相对于读取短信来说就复杂非常多了,我 ...
- 017 Android 获取手机SIM卡序列号和读取联系人
1.获取手机SIM卡序列号 //5.存储sim卡系列号 //5.1获取sim卡系列号 TelephonyManager manager = (TelephonyManager) getSystemSe ...
- Android根据联系人姓名首字符顺序读取通讯录
Android根据联系人姓名首字符顺序读取通讯录 版权声明:本文为Zhang Phil原创文章,欢迎转载!转载请注明出处:http://blog.csdn.net/zhangphil 本文给出了A ...
- Android获取联系人示例,从数据库加载,带首字母标签
这几天打算学习下Android联系人方便的一些东西,之前稍有涉略,不过每次都是浅尝辄止. 推荐国内两个Link: http://fanfq.iteye.com/blog/779569 http://w ...
- Android系统联系人全特效实现(上),分组导航和挤压动画
记得在我刚接触Android的时候对系统联系人中的特效很感兴趣,它会根据手机中联系人姓氏的首字母进行分组,并在界面的最顶端始终显示一个当前的分组.如下图所示: 最让我感兴趣的是,当后一个分组和前一个分 ...
- Android:联系人Contacts之ContentResolver query 参数详解
注:本片整理自 http://blog.csdn.net/wssiqi/article/details/8132603 1.获取联系人姓名 一个简单的例子,这个函数获取设备上所有的联系人ID和联系人N ...
随机推荐
- 【收藏】SQL多行变一列
CREATE TABLE DEPT (DeptNo INT IDENTITY(1, 1)NOT NULL , Country VARCHAR(50) , Location VARCHAR(50) ...
- VMware虚拟机安装linux7并设置网络
1.下载VMware虚拟机 https://www.vmware.com/cn/products/workstation/workstation-evaluation.html 永久激活12位序列号: ...
- day21 git & github + Celery 分布式任务队列
参考博客: git & github 快速入门http://www.cnblogs.com/alex3714/articles/5930846.html git@github.com:liyo ...
- vue组件间传值
父传子 1.父组件:在子组件容器内绑定数据 <router-view :unusedOrderNum="num1" :usedOrderNum="num2" ...
- Python之Fabric
[Fabric] Fabric是一个用Python开发的部署工具,最大特点是不用登录远程服务器,在本地运行远程命令,几行Python脚本就可以轻松部署. 安装 wget https://bootstr ...
- 【javascript基础】运算符优先级
优先级 运算类型 关联性 运算符 1 成员运算符 从左到右 . [] new 从右到左 new 2 函数调用运算符 从左到右 () 3 自增运算符 n/a ++ 自减运算符 n/a -- 4 逻辑非运 ...
- Easyui datagrid自定义排序
做项目遇到个关于排序问题,想着在前端排序,正好Easyui有这个功能,所以就拿来用了一下,因为跟官网的Demo不太一样,所以总结一下: 首先这一列是要排序的列(当然,在生产环境,这一列是隐藏的,在开发 ...
- Xcode Server (Xcode9)搭建CI
Xcode 9将Xcode Server集成进来了,这是Xcode一个新特性,不用去单独下载server了,server可以用来做CI.自动化Test等等.这里主要介绍搭建CI,相当简单 打开开关,新 ...
- Frame-Relay交换机
- windows 10 安装 sql 2005 安装失败
windows 10 安装 sql 2005 安装失败 网上的方法记录: 安装中无法启动需要先用sp4的补丁文件sqlos.dll,sqlservr.exe 替换D:\Program Files (x ...