(转)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 ...
随机推荐
- 极小极大搜索 的个人理解(alpha-beta剪枝)
极小极大搜索的算法过程: 参考文档:http://www.xqbase.com/computer/search_minimax.htm (经典) 主要思想比较简单,但说清楚也不大容易.其核心思想是通过 ...
- 初次使用Bootstrap
一.概述也好,过程也好,反正就是这样 知道这个还是从群里听到的,一个人的视野是有限,必须借助他人来扩展.好奇使我去搜索了解了一下.原来是一个网页前端框架,可以适合不同大小屏幕,自动适应正常显示.这就是 ...
- 关于px em rem的一点小总结
2015-11-28 06:06:40 概念 都是CSS单位. px:像素 Pixel.像素 (计算机屏幕上的一个点) em:1em 等于当前的字体尺寸. 2em 等于当前字体尺寸的两倍. 例如,如果 ...
- @Transactional的readOnly、timeout
1.@Transactional的readOnly 在使用@Transactional注解的时候,有一个属性是readOnly,默认值是false readOnly的意思就是当前的方法是只读的,也就是 ...
- 《Effective C++》第3章 资源管理(2)-读书笔记
章节回顾: <Effective C++>第1章 让自己习惯C++-读书笔记 <Effective C++>第2章 构造/析构/赋值运算(1)-读书笔记 <Effecti ...
- MoreEffectiveC++Item35(基础议题)(条款1-4)
条款1:区别指针和引用 条款2:最好使用C++转换操作符 条款3: 绝对不要以多态的方式处理数组 条款4: 避免无用的缺省构造函数 条款1:区别指针和引用 1.指针(pointer) 使用[*/-&g ...
- Linux之sshd服务
---恢复内容开始--- ---恢复内容结束--- 一.linux中对服务管理与控制(以sshd为例) 1.什么是服务 可以用来给客户提供相关操作,对自己没有什么好处 2.用什么控制服务 系统初始化进 ...
- jquery过滤特殊字符及js字符串转为数字
//替换特殊字符 $(this).val($(this).val().replace(/[~'!<>@#$%^&*()-+_=:]/g, "")); 方法主要有 ...
- Android Bluetooth 总结
一.Android Bluetooth现状 (1)Android2.2版 支持的蓝牙核心版本是Bluetooth 2.0 + EDR. (2)Android 的蓝牙 使用了BlueZ协议栈,但只实现了 ...
- iot_programe Makefile hacking
/***************************************************************************** * iot_programe Makefi ...