Android,通讯录导入,contacts,联系人

来源:互联网 发布:java微信后台开发 编辑:程序博客网 时间:2024/05/18 01:28

说明 :  APP 端有联系人的信息,要导入到手机

   (这里只是抽取了项目中小部分的代码,如果有这方面的需求,直接拷贝即可)

 1.  获取某个分组下的所有联系人信息(模板,可以不做修改直接使用) 

         拷贝之后,报红部分是一个联系人的Bean,修改成自己的...
public List<AppAddressBook> getAllContactsByGroupId(long groupId) {        String[] RAW_PROJECTION = new String[]{ContactsContract.Data.RAW_CONTACT_ID,};        String RAW_CONTACTS_WHERE = ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID                + "=?"                + " and "                + ContactsContract.Data.MIMETYPE                + "="                + "'"                + ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE                + "'";        // 通过分组的id 查询得到RAW_CONTACT_ID        Cursor cursor = MainActivity.this.getContentResolver().query(                ContactsContract.Data.CONTENT_URI, RAW_PROJECTION,                RAW_CONTACTS_WHERE, new String[]{groupId + ""}, "data1 asc");        List<AppAddressBook> contactList = new ArrayList<AppAddressBook>();        while (cursor.moveToNext()) {            // RAW_CONTACT_ID            int col = cursor.getColumnIndex("raw_contact_id");            int raw_contact_id = cursor.getInt(col);            mAppAddressBook = new AppAddressBook();            mAppAddressBook.setUserId(raw_contact_id);            Uri dataUri = Uri.parse("content://com.android.contacts/data");            Cursor dataCursor = MainActivity.this.getContentResolver().query(dataUri,                    null, "raw_contact_id=?",                    new String[]{raw_contact_id + ""}, null);            while (dataCursor.moveToNext()) {                String data1 = dataCursor.getString(dataCursor                        .getColumnIndex("data1"));                String mime = dataCursor.getString(dataCursor                        .getColumnIndex("mimetype"));                if ("vnd.android.cursor.item/phone_v2".equals(mime)) {                    mAppAddressBook.setPhone1(data1); // 设置电话号码,就是手机号码                } else if ("vnd.android.cursor.item/name".equals(mime)) {                    mAppAddressBook.setUserName(data1); // 设置姓名                }            }            dataCursor.close();            contactList.add(mAppAddressBook);            mAppAddressBook = null;        }        cursor.close();        return contactList;    }


  2. 根据姓名删除联系人  (模板,可以不做修改直接使用)

              有时候根据姓名删除联系人并不合适,所以可以选择根据 ID 来删除联系人

                  做法 : 可以在方法里面增加一个参数 :

                         deleteLocalContact(Context context, String name , int currentId )
                  在这里: long id = cursor.getLong(cursor.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID)) ;下面添加一个判断

                              if (currentId == id ) {

                                  ops.add(ContentProviderOperation.newDelete(

                                                 ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, Id)).build());

                                 try {

                                      getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops) ;

                                    } catch (Exception e) {

                               }

          

       (这个部分可以直接拷贝,都是原生API) 如果你想单纯根据姓名删除联系人,直接拷贝这部分就成

 private void deleteLocalContact(Context context, String name) {        Cursor cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,                new String[]{ContactsContract.Data.RAW_CONTACT_ID},                ContactsContract.Contacts.DISPLAY_NAME + "=?", new String[]{name}, null);        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();        if (cursor.moveToFirst()) {            do {                long Id = cursor.getLong(cursor.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID));                ops.add(ContentProviderOperation.newDelete(                        ContentUris.withAppendedId(ContactsContract.RawContacts.CONTENT_URI, Id)).build());                try {                    getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);                } catch (Exception e) {                }            } while (cursor.moveToNext());            cursor.close();        }    }



0 0
原创粉丝点击