(转)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 ...
随机推荐
- Java集合详解3:Iterator,fail-fast机制与比较器
Java集合详解3:Iterator,fail-fast机制与比较器 今天我们来探索一下LIterator,fail-fast机制与比较器的源码. 具体代码在我的GitHub中可以找到 https:/ ...
- PANDAS 数据合并与重塑(concat篇)
转自:http://blog.csdn.net/stevenkwong/article/details/52528616
- 【Windows】netsh动态配置端口转发
文章转载自傲风 使用多个虚拟机,将开发环境和工作沟通环境分开(即时通,办公系统都只能在windows下使用-),将开发环境的服务提供给外部访问时,需要在主机上通过代理配置数据转发. VirtualBo ...
- Highcharts 时间序列,可缩放的图表;Highcharts X 轴翻转曲线图;Highcharts 带标记曲线图
Highcharts 时间序列,可缩放的图表 配置 图表 配置可缩放图表. chart.zoomType 指定了用户可以拖放的尺寸,用户可以通过拖动鼠标来放大,可能值是x,y或xy: var char ...
- This function has none of DETERMINISTIC, NO SQL解决办法
This function has none of DETERMINISTIC, NO SQL解决办法 创建存储过程时 出错信息: ERROR 1418 (HY000): This function ...
- pxe自动安装
首先我们要用到的linux的桌面模式,没有桌面模式的请自觉安装.注意要关闭防火墙 需要的支持环境,dhcp,tftp,vsftp. 1.先创建ftp的共享光盘目录 mkdir -p /var/ftp/ ...
- netcat 安装 及netcat 命名介绍
netcat 安装 https://blog.csdn.net/transformer_wsz/article/details/77624087 http://www.cnblogs.com/php- ...
- CMDB配置资源管理数据库(理解)
CMDB是运维自动化的基础,它为日志系统,发布系统,监控系统等运维系统(ELK,zabbix,open-falcon)提供接口函数, 第一种方式:Agent方法实现,agent不能直接访问数据库,因为 ...
- js中Function的apply方法与call方法理解
最近在使用jQuery的$.each方法时很,突然想到$.each($('div'),function(index,entity){});中的这个index和entity是哪冒出来的,而且可有可无的, ...
- 《Drools7.0.0.Final规则引擎教程》第4章 4.4 约束(Pattern的一部分)
4.4.3 约束(Pattern的一部分) 前面我们已经介绍了条件约束在Pattern中位置了,那么什么是条件约束呢?简单来说就是一个返回true或者false的表达式,比如下面的5小于6,就是一个约 ...