// 根据ID获得联系人信息实体 private static EnContactBackUp getEnContactBackUp(String id) { ContentResolver resolver = acApplication.getInstance() .getContentResolver(); EnContactBackUp contact = new EnContactBackUp(); contact.setId(id); String value = null; // 获得昵称 Cursor cur = resolver.query(ContactsContract.Data.CONTENT_URI, new String[] { Nickname.NAME }, ContactsContract.Data.CONTACT_ID + "=? and " + ContactsContract.Data.MIMETYPE + "=?", new String[] { id, Nickname.CONTENT_ITEM_TYPE }, null); while (cur.moveToNext()) { contact.setNickName(cur.getString(0).replaceAll("#", "#") .replaceAll(",", ",").replaceAll(";", ";") .replaceAll(":", ":")); } cur.close(); // 获得名字序列串 Cursor nameCur = resolver.query(ContactsContract.Data.CONTENT_URI, new String[] { ContactsContract.Data.DATA4, ContactsContract.Data.DATA3, ContactsContract.Data.DATA2, ContactsContract.Data.DATA5, ContactsContract.Data.DATA6 }, ContactsContract.Data.CONTACT_ID + "=? and " + ContactsContract.Data.MIMETYPE + "=?", new String[] { id, StructuredName.CONTENT_ITEM_TYPE }, null); while (nameCur.moveToNext()) { value = nameCur.getString(0); contact.setPrefix(value != null ? value.replaceAll("#", "#") .replaceAll(",", ",").replaceAll(";", ";") .replaceAll(":", ":").replace(" ", " ") : value); value = nameCur.getString(1); contact.setFamilyName(value != null ? value.replaceAll("#", "#") .replaceAll(",", ",").replaceAll(";", ";") .replaceAll(":", ":").replace(" ", " ") : value); value = nameCur.getString(2); contact.setGivenName(value != null ? value.replaceAll("#", "#") .replaceAll(",", ",").replaceAll(";", ";") .replaceAll(":", ":").replace(" ", " ") : value); value = nameCur.getString(3); contact.setCenterName(value != null ? value.replaceAll("#", "#") .replaceAll(",", ",").replaceAll(";", ";") .replaceAll(":", ":").replace(" ", " ") : value); value = nameCur.getString(4); contact.setSuffix(value != null ? value.replaceAll("#", "#") .replaceAll(",", ",").replaceAll(";", ";") .replaceAll(":", ":").replace(" ", " ") : value); } nameCur.close(); // 根据ID查询通讯录号码 Cursor phoneCursor = resolver.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.TYPE }, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?", new String[] { id }, null); System.out.println("phone count " + phoneCursor.getCount()); while (phoneCursor.moveToNext()) { int type = phoneCursor.getInt(1); value = phoneCursor.getString(0); switch (type) { case Phone.TYPE_HOME: contact.setHomeTel(value != null ? value.replaceAll("#", "#") .replaceAll(",", ",").replaceAll(";", ";") .replaceAll(":", ":").replace(" ", " ") : value); break; case Phone.TYPE_MOBILE: contact.setUsePhone(value != null ? value.replaceAll("#", "#") .replaceAll(",", ",").replaceAll(";", ";") .replaceAll(":", ":").replace(" ", " ") : value); break; case Phone.TYPE_WORK: contact.setOfficeTel(value != null ? value.replaceAll("#", "#") .replaceAll(",", ",").replaceAll(";", ";") .replaceAll(":", ":").replace(" ", " ") : value); break; case Phone.TYPE_FAX_HOME: contact.setHomeFax(value != null ? value.replaceAll("#", "#") .replaceAll(",", ",").replaceAll(";", ";") .replaceAll(":", ":").replace(" ", " ") : value); break; case Phone.TYPE_FAX_WORK: contact.setOfficeFax(value != null ? value.replaceAll("#", "#") .replaceAll(",", ",").replaceAll(";", ";") .replaceAll(":", ":").replace(" ", " ") : value); break; } } phoneCursor.close(); // 关闭游标 // 获得QQ号 Cursor imCur = resolver.query(ContactsContract.Data.CONTENT_URI, new String[] { Im.DATA, Im.PROTOCOL }, ContactsContract.Data.CONTACT_ID + "=? and " + ContactsContract.Data.MIMETYPE + "=?", new String[] { id, Im.CONTENT_ITEM_TYPE }, null); while (imCur.moveToNext()) { int type = imCur.getInt(1); value = imCur.getString(0); switch (type) { case Im.PROTOCOL_QQ: // 匹配为QQ contact.setQQNum(value != null ? value.replaceAll("#", "#") .replaceAll(",", ",").replaceAll(";", ";") .replaceAll(":", ":").replace(" ", " ") : value); break; } } imCur.close(); // 获得职位和公司 Cursor orgCur = resolver.query(ContactsContract.Data.CONTENT_URI, new String[] { Organization.COMPANY, Organization.TITLE, Organization.TYPE }, ContactsContract.Data.CONTACT_ID + "=? and " + ContactsContract.Data.MIMETYPE + "=?", new String[] { id, Organization.CONTENT_ITEM_TYPE }, null); while (orgCur.moveToNext()) { int type = orgCur.getInt(2); switch (type) { case Organization.TYPE_WORK: value = orgCur.getString(0); contact.setCompany(value != null ? value.replaceAll("#", "#") .replaceAll(",", ",").replaceAll(";", ";") .replaceAll(":", ":").replace(" ", " ") : value); value = orgCur.getString(1); contact.setPisition(value != null ? value.replaceAll("#", "#") .replaceAll(",", ",").replaceAll(";", ";") .replaceAll(":", ":").replace(" ", " ") : value); break; } } orgCur.close(); // 获得备注 Cursor noteCur = resolver.query(ContactsContract.Data.CONTENT_URI, new String[] { Note.NOTE }, ContactsContract.Data.CONTACT_ID + "=? and " + ContactsContract.Data.MIMETYPE + "=?", new String[] { id, Note.CONTENT_ITEM_TYPE }, null); while (noteCur.moveToNext()) { value = noteCur.getString(0); contact.setNote(value != null ? value.replaceAll("#", "#") .replaceAll(",", ",").replaceAll(";", ";") .replaceAll(":", ":").replace(" ", " ") : value); } noteCur.close(); // 获得邮箱信息 Cursor emailCur = resolver.query( ContactsContract.CommonDataKinds.Email.CONTENT_URI, new String[] { Email.DATA1, Email.TYPE }, ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=? and " + ContactsContract.CommonDataKinds.Email.MIMETYPE + "=?", new String[] { id, Email.CONTENT_ITEM_TYPE }, null); while (emailCur.moveToNext()) { int type = emailCur.getInt(1); value = emailCur.getString(0); switch (type) { case Email.TYPE_HOME: contact.setHomeMailBox(value != null ? value .replaceAll("#", "#").replaceAll(",", ",") .replaceAll(";", ";").replaceAll(":", ":") .replace(" ", " ") : value); break; case Email.TYPE_WORK: contact.setOfficeMailBox(value != null ? value .replaceAll("#", "#").replaceAll(",", ",") .replaceAll(";", ";").replaceAll(":", ":") .replace(" ", " ") : value); break; case Email.TYPE_OTHER: contact.setElecMailBox(value != null ? value .replaceAll("#", "#").replaceAll(",", ",") .replaceAll(";", ";").replaceAll(":", ":") .replace(" ", " ") : value); break; } } emailCur.close(); // 获得网址 Cursor webCur = resolver.query(ContactsContract.Data.CONTENT_URI, new String[] { Website.URL, Website.TYPE }, ContactsContract.CommonDataKinds.Website.CONTACT_ID + "=? and " + ContactsContract.CommonDataKinds.Website.MIMETYPE + "=?", new String[] { id, Website.CONTENT_ITEM_TYPE }, null); while (webCur.moveToNext()) { int type = webCur.getInt(1); switch (type) { case Website.TYPE_OTHER: value = webCur.getString(0); contact.setPersonWeb(value != null ? value.replaceAll("#", "#") .replaceAll(",", ",").replaceAll(";", ";") .replaceAll(":", ":").replace(" ", " ") : value); break; } } webCur.close(); return contact; } // 通讯录还原 添加联系人 public static long updateContact(EnContactBackUp contact) { // 获得查询分析器 ContentValues values = new ContentValues(); ContentResolver res = acApplication.getInstance().getContentResolver(); String value = null; // 插入通讯录 Uri rawContactUri = res.insert( ContactsContract.RawContacts.CONTENT_URI, values); long rawContactId = ContentUris.parseId(rawContactUri); values.clear(); value = contact.getNickName(); if (value != null && !value.equals("") && !value.equals(" ")) { // 设置昵称 values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId); values.put(Nickname.NAME, value); values.put(Data.MIMETYPE, Nickname.CONTENT_ITEM_TYPE); res.insert(ContactsContract.Data.CONTENT_URI, values); values.clear(); } // 设置名字信息 values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE); value = contact.getPrefix(); if (value != null && !value.equals("") && !value.equals(" ")) { values.put(StructuredName.PREFIX, value); } value = contact.getFamilyName(); if (value != null && !value.equals("") && !value.equals(" ")) { values.put(StructuredName.FAMILY_NAME, value); } value = contact.getGivenName(); if (value != null && !value.equals("") && !value.equals(" ")) { values.put(StructuredName.GIVEN_NAME, value); } value = contact.getCenterName(); if (value != null && !value.equals("") && !value.equals(" ")) { values.put(StructuredName.MIDDLE_NAME, value); } value = contact.getSuffix(); if (value != null && !value.equals("") && !value.equals(" ")) { values.put(StructuredName.SUFFIX, value); } res.insert(ContactsContract.Data.CONTENT_URI, values); values.clear(); // 添加手机号码 value = contact.getUsePhone(); if (value != null && !value.equals("")) { values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, value); values.put(ContactsContract.CommonDataKinds.Phone.TYPE, Phone.TYPE_MOBILE); res.insert(ContactsContract.Data.CONTENT_URI, values); values.clear(); } // 添加家庭号码 value = contact.getHomeTel(); if (value != null && !value.equals("")) { values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId); values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, value); values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); values.put(ContactsContract.CommonDataKinds.Phone.TYPE, Phone.TYPE_HOME); res.insert(ContactsContract.Data.CONTENT_URI, values); values.clear(); } // 添加单位号码 value = contact.getOfficeTel(); if (value != null && !value.equals("")) { values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, value); values.put(ContactsContract.CommonDataKinds.Phone.TYPE, Phone.TYPE_WORK); res.insert(ContactsContract.Data.CONTENT_URI, values); values.clear(); } // 添加家庭传真 value = contact.getHomeFax(); if (value != null && !value.equals("")) { values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId); values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, value); values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); values.put(ContactsContract.CommonDataKinds.Phone.TYPE, Phone.TYPE_FAX_HOME); res.insert(ContactsContract.Data.CONTENT_URI, values); values.clear(); } // 添加办公传真 value = contact.getOfficeFax(); if (value != null && !value.equals("")) { values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId); values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, value); values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); values.put(ContactsContract.CommonDataKinds.Phone.TYPE, Phone.TYPE_FAX_WORK); res.insert(ContactsContract.Data.CONTENT_URI, values); values.clear(); } // 添加QQ号码 value = contact.getQQNum(); if (value != null && !value.equals("")) { values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId); values.put(ContactsContract.Data.MIMETYPE, Im.CONTENT_ITEM_TYPE); values.put(Im.DATA, value); values.put(Im.PROTOCOL, Im.PROTOCOL_QQ); res.insert(ContactsContract.Data.CONTENT_URI, values); values.clear(); } // 添加公司和职位 if ((contact.getCompany() != null && !contact.getCompany().equals("")) || (contact.getPisition() != null && !contact.getPisition() .equals(""))) { values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId); values.put(ContactsContract.Data.MIMETYPE, Organization.CONTENT_ITEM_TYPE); if (contact.getCompany() != null && !contact.getCompany().equals("")) { values.put(Organization.COMPANY, contact.getCompany()); } if (contact.getPisition() != null || !contact.getPisition().equals("")) { values.put(Organization.TITLE, contact.getPisition()); } values.put(Organization.TYPE, Organization.TYPE_WORK); res.insert(ContactsContract.Data.CONTENT_URI, values); values.clear(); } // 添加备注 value = contact.getNote(); if (value != null && !value.equals("")) { values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId); values.put(ContactsContract.Data.MIMETYPE, Note.CONTENT_ITEM_TYPE); values.put(Note.NOTE, value); res.insert(ContactsContract.Data.CONTENT_URI, values); values.clear(); } // 添加电子邮箱 value = contact.getElecMailBox(); if (value != null && !value.equals("")) { values.put(ContactsContract.CommonDataKinds.Email.RAW_CONTACT_ID, rawContactId); values.put(ContactsContract.Data.MIMETYPE, Email.CONTENT_ITEM_TYPE); values.put(Email.DATA1, value); values.put(Email.TYPE, Email.TYPE_OTHER); res.insert(ContactsContract.Data.CONTENT_URI, values); values.clear(); } // 添加家庭邮箱 value = contact.getHomeMailBox(); if (value != null && !value.equals("")) { values.put(ContactsContract.CommonDataKinds.Email.RAW_CONTACT_ID, rawContactId); values.put(ContactsContract.Data.MIMETYPE, Email.CONTENT_ITEM_TYPE); values.put(Email.DATA1, value); values.put(Email.TYPE, Email.TYPE_HOME); res.insert(ContactsContract.Data.CONTENT_URI, values); values.clear(); } // 添加单位邮箱 value = contact.getOfficeMailBox(); if (value != null && !value.equals("")) { values.put(ContactsContract.CommonDataKinds.Email.RAW_CONTACT_ID, rawContactId); values.put(ContactsContract.Data.MIMETYPE, Email.CONTENT_ITEM_TYPE); values.put(Email.DATA1, value); values.put(Email.TYPE, Email.TYPE_WORK); res.insert(ContactsContract.Data.CONTENT_URI, values); values.clear(); } // 添加网站 value = contact.getPersonWeb(); if (value != null && !value.equals("")) { values.put(ContactsContract.CommonDataKinds.Website.RAW_CONTACT_ID, rawContactId); values.put(ContactsContract.Data.MIMETYPE, Website.CONTENT_ITEM_TYPE); values.put(Website.URL, value); values.put(Website.TYPE, Website.TYPE_OTHER); res.insert(ContactsContract.Data.CONTENT_URI, values); values.clear(); } return rawContactId; }