通过ContentResolver对手机本地联系人进行增删改查

增加手机本地联系人

try {

  ContentValues values =new ContentValues();

  //首先向RawContacts.CONTENT_URI执行一个空值插入,目的是获取系统返回的rawContactId

  Uri rawContactUri =context.getContentResolver().insert(ContactsContract.RawContacts.CONTENT_URI, values);

  //获取id

   long  rawContactId = ContentUris.parseId(rawContactUri);

  //往data表入姓名数据

    values.clear();

    values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);

    values.put(ContactsContract.Data.MIMETYPE,       ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);

    values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,“name”);

    values.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,"firstName");

    values.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,"lastName");

    context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);

    //写入头像

    values.clear();

    values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);

    values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);

    values.put(ContactsContract.CommonDataKinds.Photo.PHOTO,headImageByte);

    context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);

 //写入手机号码

  values.clear();

  values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);

  values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);

  values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, "phone");

  values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);

  context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);

  //写入住宅电话

  values.clear();

  values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);

  values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);

  values.put(ContactsContract.CommonDataKinds.Phone.NUMBER,  "phone");

  values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_HOME);

  context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);

  //写入商务电话

  values.clear();

  values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);

  values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);

  values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, "phone");

  values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE);

  context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);

  //写入商务传真

  values.clear();

  values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);

  values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);

  values.put(ContactsContract.CommonDataKinds.Phone.NUMBER,  "phone");

  values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK);

  context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);

  // 联系人公司和职位

  values.clear();

  values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);

  values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);

  values.put(ContactsContract.CommonDataKinds.Organization.COMPANY, "company");

  context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);

  // 插入Email数据

  values.clear();

  values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);

  values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);

  values.put(ContactsContract.CommonDataKinds.Email.DATA, "email");

  values.put(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK);

  context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);

删除手机本地联系人

ContentResolver resolver =context.getContentResolver();

//删除Contacts表中的数据

int delete =0;

try {

delete = resolver.delete(ContactsContract.Contacts.CONTENT_URI, ContactsContract.Contacts._ID +" =?", new String[]{String.valueOf(contactsDTO.getId())});

}catch (Exception e) {}

//删除RawContacts表的数据

int delete3 =0;

try {

delete3 = resolver.delete(ContactsContract.RawContacts.CONTENT_URI, ContactsContract.RawContacts.CONTACT_ID +" =?", new String[]{String.valueOf(contactsDTO.getId())});

}catch (Exception e) {}

//删除姓名

int delete4 =0;

try {

delete4 = resolver.delete(ContactsContract.RawContacts.CONTENT_URI, "display_name=?", new String[]{contactsDTO.getName()});

}catch (Exception e) {}

修改手机本地联系人

try {

//获取要修改的联系人的rawContactId ,然后根据rawContactId 进行修改信息

ContentResolver contentResolver =context.getContentResolver();

  Cursor cursor = contentResolver.query(ContactsContract.RawContacts.CONTENT_URI, null, ContactsContract.RawContacts.CONTACT_ID +"=?", new String[]{“id”}, null);

  int rawContactId = -1;

  while (cursor.moveToNext()) {

rawContactId = cursor.getInt(cursor.getColumnIndexOrThrow(ContactsContract.RawContacts._ID));

  }

cursor.close();

  cursor =null;

  ContentValues values =new ContentValues();

  //修改全名,名字和姓氏

  values.clear();

  values.put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,“name”);

  values.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,"firstName");

  values.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, "lastname");

  contentResolver.update(ContactsContract.Data.CONTENT_URI, values, ContactsContract.Data.CONTACT_ID +"=?", new String[]{"id"});

  //修改地址

  values.clear();

  values.put(ContactsContract.CommonDataKinds.Organization.COMPANY, "company");

  contentResolver.update(ContactsContract.Data.CONTENT_URI, values, ContactsContract.Data.CONTACT_ID +"=? and " + ContactsContract.Data.MIMETYPE +"=?", new String[]{"id", ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE});

  //修改邮箱

  values.clear();

  values.put(ContactsContract.CommonDataKinds.Email.ADDRESS, "email");

  contentResolver.update(ContactsContract.Data.CONTENT_URI, values, ContactsContract.Data.CONTACT_ID +"=? and " + ContactsContract.Data.MIMETYPE +"=?", new String[]{"id", ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE});

  //修改头像

  values.clear();

  values.put(ContactsContract.CommonDataKinds.Photo.PHOTO, headImagebyte);

  contentResolver.update(ContactsContract.Data.CONTENT_URI, values, ContactsContract.Data.CONTACT_ID +"=? and " + ContactsContract.Data.MIMETYPE +"=?", new String[]{"id", ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE});


//这里修改手机号的逻辑是先通过update将之前手机号的数据设置为空,然后通过insert再出入新的手机号;通过代码获取联系人手机号数据是对的,但是在手机通讯里看联系人,发现联系人会有多条手机号为空的条目,(没办法,暂时不知道该怎么直接update手机号,┭┮﹏┭┮)


  //修改手机号  

values.clear();

  values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, "");

  values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);

  contentResolver.update(ContactsContract.Data.CONTENT_URI, values, ContactsContract.Data.MIMETYPE +"=? and " + ContactsContract.Data.RAW_CONTACT_ID +"=?", new String[]{ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, rawContactId +""});

  values.clear();

  values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, "");

  values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_HOME);

  contentResolver.update(ContactsContract.Data.CONTENT_URI, values, ContactsContract.Data.MIMETYPE +"=? and " + ContactsContract.Data.RAW_CONTACT_ID +"=?", new String[]{ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, rawContactId +""});

  values.clear();

  values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, "");

  values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE);

  contentResolver.update(ContactsContract.Data.CONTENT_URI, values, ContactsContract.Data.MIMETYPE +"=? and " + ContactsContract.Data.RAW_CONTACT_ID +"=?", new String[]{ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, rawContactId +""});

  values.clear();

  values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, "");

  values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK);

  contentResolver.update(ContactsContract.Data.CONTENT_URI, values, ContactsContract.Data.MIMETYPE +"=? and " + ContactsContract.Data.RAW_CONTACT_ID +"=?", new String[]{ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, rawContactId +""});

  values.clear();

  values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);

  values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);

  values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, “phone”);

  values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);

  context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);

  values.clear();

  values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);

  values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);

  values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, “phone”);

  values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_HOME);

  context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);

  values.clear();

  values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);

  values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);

  values.put(ContactsContract.CommonDataKinds.Phone.NUMBER, “phone”);

  values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE);

  context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);

  values.clear();

  values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);

  values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);

  values.put(ContactsContract.CommonDataKinds.Phone.NUMBER,“phone”);

  values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK);

  context.getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);

查询手机本地联系人

Cursor cursor =context.getContentResolver().query(

ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

        while (cursor.moveToNext()) {

          //新建一个联系人实例

          ContactsDTO temp =new ContactsDTO();

//获取联系人id

          String contactId = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

          temp.id = contactId;

//获取联系人name

          Cursor nameCur =context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, ContactsContract.Data.CONTACT_ID +"=" + contactId, null, null);

          if (nameCur.moveToNext()) {

String given = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME));

            String family = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME));

            temp.setFirst_name(given);

            temp.setLast_name(family);

          }

nameCur.close();

String contactName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));

          temp.setName(contactName);

//获取联系人头像

          try {

temp.head_img =getContactsIcon(context, contactId);

          }catch (IOException e) {

e.printStackTrace();

          }

//获取联系人邮箱

Cursor email =context.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID +" = " + contactId, null, null);

          while (email.moveToNext()) {

String mail = email.getString(email.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

            temp.setEmail(mail);

          }

email.close();

//获取联系人公司

          String orgWhere = ContactsContract.Data.CONTACT_ID +" = ? AND " + ContactsContract.Data.MIMETYPE +" = ?";

          String[] orgWhereParams =new String[]{contactId,

            ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE};

          Cursor orgCur =context.getContentResolver().query(ContactsContract.Data.CONTENT_URI,

            null, orgWhere, orgWhereParams, null);

          if (orgCur.moveToFirst()) {

//组织名 (公司名字)

            String company = orgCur.getString(orgCur.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));

            temp.setCompany(company);

          }

orgCur.close();

          //获取联系人电话号码

          Cursor phoneCursor =context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,

            null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +"=" + contactId, null, null);

          while (phoneCursor.moveToNext()) {

int pN = phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

            String phone = phoneCursor.getString(pN);

            int pT = phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);

            String phoneType = phoneCursor.getString(pT);

            phone = phone.replace("-", "");

            phone = phone.replace(" ", "");

            if (StringUtils.parseIntByString(phoneType) == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) {

temp.setMobile_phone(phone);

            }else if (StringUtils.parseIntByString(phoneType) == ContactsContract.CommonDataKinds.Phone.TYPE_HOME) {

temp.setHome_phone(phone);

            }else if (StringUtils.parseIntByString(phoneType) == ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME) {

temp.setHome_fax(phone);

            }else if (StringUtils.parseIntByString(phoneType) == ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE) {

temp.setBusiness(phone);

            }else if (StringUtils.parseIntByString(phoneType) == ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK) {

temp.setBusiness_fax(phone);

            }

}

//记得要把cursor给close掉

          phoneCursor.close();

        }

cursor.close();


/**

  * 获取联系人头像

  *

  * @param context

  * @return

  */

  public static byte[]getContactsIcon(Context context, String contactsId)throws IOException {

// 获取内容解析者

    ContentResolver contentResolver = context.getContentResolver();

    // 查头像要传的uri 参1 baseuri 参2 要拼接的部分

    Uri contactUri = Uri.withAppendedPath(

ContactsContract.Contacts.CONTENT_URI, contactsId);

    //获取联系人头像的流

    InputStream iconIs = ContactsContract.Contacts

.openContactPhotoInputStream(contentResolver, contactUri);

    if (null == iconIs) {

return null;

    }

    return toByteArray(iconIs);

  }


public static byte[]toByteArray(InputStream input)throws IOException {

ByteArrayOutputStream output =new ByteArrayOutputStream();

  byte[] buffer =new byte[4096];

  int n =0;

  while (-1 != (n = input.read(buffer))) {

output.write(buffer, 0, n);

  }

return output.toByteArray();

}

你可能感兴趣的:(通过ContentResolver对手机本地联系人进行增删改查)