2021-10-18

2021-10-18,第1张

2021-10-18 Android完整的通讯录获取多个手机号码、多个邮箱、多个地址、生日、备份等信息的代码 整个删除通讯录 方法一:
 //删除整个通讯录
    public static void deleteAllContacts(Context context) {
        ContentResolver cr = context.getContentResolver();
        Uri uri4 = ContactsContract.RawContacts.CONTENT_URI.buildUpon().appendQueryParameter(ContactsContract.CALLER_IS_SYNCADAPTER, "true").build();
        cr.delete(uri4, null, null);
        }
方法二:
 //删除整个通讯录
    public static void deleteAllContacts(Context context) {
        ContentResolver cr = context.getContentResolver();
        // 查询contacts表的所有记录
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                null, null, null);
        // 如果记录不为空
        if (cursor.getCount() > 0) {
            // 游标初始指向查询结果的第一条记录的上方,执行moveToNext函数会判断
            // 下一条记录是否存在,如果存在,指向下一条记录。否则,返回false。
            while (cursor.moveToNext()) {
                //	String rawContactId = "";
                // 从Contacts表当中取得ContactId
//    				String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

                @SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                //根据姓名求id
                Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");

                Cursor cursor1 = cr.query(uri, new String[]{ContactsContract.Data._ID}, "display_name=?", new String[]{name}, null);
                if (cursor1.moveToFirst()) {
                    int id = cursor1.getInt(0);
                    //根据id删除data中的相应数据
                    cr.delete(uri, "display_name=?", new String[]{name});
                    uri = Uri.parse("content://com.android.contacts/data");
                    cr.delete(uri, "raw_contact_id=?", new String[]{id + ""});
                 }
                cursor1.close();
             }
          }
          cursor.close();
        }
获取通讯录姓名、id、多个手机号码、多个邮箱、多个地址、生日、备注信息
 private static List list;

    @SuppressLint("Range")
    public static void fetchContact(Context context) {
        list = new ArrayList<>();

        ContentResolver cr = context.getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                ContactsBean contactClass = new ContactsBean();
                contactClass.id = id;
                contactClass.name = name;
                if (cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
                    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                            new String[]{id}, null);

                    contactClass.phone = new ContactsBean.Phone();
                    //获取手机号码
                    while (pCur.moveToNext()) {
                        String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        int type = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

                        switch (type) {
                            case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
                                try {
                                    if (phoneNo.equals("") || phoneNo != null) {
                                        contactClass.phone.home = phoneNo;
                                    }
                                } catch (Exception e) {
                                    Log.d(TAG, e.toString());
                                }
                                break;
                            case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
                                try {
                                    if (phoneNo.equals("") || phoneNo != null) {
                                        contactClass.phone.mobile1 = phoneNo;
                                    }
                                } catch (Exception e) {
                                    Log.d(TAG, e.toString());
                                }

                                break;
                            case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
                                try {
                                    if (phoneNo.equals("") || phoneNo != null) {
                                        contactClass.phone.work = phoneNo;
                                    }
                                } catch (Exception e) {
                                    Log.d(TAG, e.toString());
                                }

                                break;
                            case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME:
                                try {
                                    if (phoneNo.equals("") || phoneNo != null) {
                                        contactClass.phone.faxhome = phoneNo;
                                    }
                                } catch (Exception e) {
                                    Log.d(TAG, e.toString());
                                }

                                break;
                            case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK:
                                try {
                                    if (phoneNo.equals("") || phoneNo != null) {
                                        contactClass.phone.faxwork = phoneNo;
                                    }
                                } catch (Exception e) {
                                    Log.d(TAG, e.toString());
                                }

                                break;
                            case ContactsContract.CommonDataKinds.Phone.TYPE_MAIN:
                                try {
                                    if (phoneNo.equals("") || phoneNo != null) {
                                        contactClass.phone.main = phoneNo;
                                    }
                                } catch (Exception e) {
                                    Log.d(TAG, e.toString());
                                }

                                break;
                            case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER:
                                try {
                                    if (phoneNo.equals("") || phoneNo != null) {
                                        contactClass.phone.other = phoneNo;
                                    }
                                } catch (Exception e) {
                                    Log.d(TAG, e.toString());
                                }

                                break;
                            case ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM:
                                try {
                                    if (phoneNo.equals("") || phoneNo != null) {
                                        contactClass.phone.custom = phoneNo;
                                    }
                                } catch (Exception e) {
                                    Log.d(TAG, e.toString());
                                }

                                break;
                            case ContactsContract.CommonDataKinds.Phone.TYPE_PAGER:
                                try {
                                    if (phoneNo.equals("") || phoneNo != null) {
                                        contactClass.phone.pager = phoneNo;
                                    }
                                } catch (Exception e) {
                                    Log.d(TAG, e.toString());
                                }
                                break;
                        }
                    }
                    pCur.close();
                }

                //获取邮箱地址
                Cursor emailCur = cr.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + id, null, null);
                if (emailCur.getCount() > 0) {
                    contactClass.email = new ContactsBean.Email();
                    while (emailCur.moveToNext()) {
                        int emailType = emailCur.getInt(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
                        String email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                        switch (emailType) {
                            case ContactsContract.CommonDataKinds.Email.TYPE_HOME:
                                contactClass.email.home = email;
                                break;
                            case ContactsContract.CommonDataKinds.Email.TYPE_WORK:
                                contactClass.email.work = email;
                                break;
                            case ContactsContract.CommonDataKinds.Email.TYPE_OTHER:
                                contactClass.email.other = email;
                                break;
                            case ContactsContract.CommonDataKinds.Email.TYPE_MOBILE:
                                contactClass.email.mobile = email;
                                break;
                            default:
                                throw new IllegalStateException("Unexpected value: " + emailType);
                        }
                        Log.e(TAG, " emailType: " + emailType + "  email: " + email);
                    }
                }
                emailCur.close();

                // 获取该联系人地址
                Cursor addressCur = cr.query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = " + id, null, null);
                if (addressCur.getCount() > 0) {
                    contactClass.address = new ContactsBean.Address();
                    while (addressCur.moveToNext()) {
                        // 遍历所有的地址
                        String country = addressCur.getString(addressCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
                        String city = addressCur.getString(addressCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
                        String street = addressCur.getString(addressCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
                        String region = addressCur.getString(addressCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
                        String postCode = addressCur.getString(addressCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
                        String formatAddress = addressCur.getString(addressCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
                        int addressType = addressCur.getInt(addressCur.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
                        switch (addressType) {
                            case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME:
                                contactClass.address.home = new ContactsBean.Address.AddressInfo();
                                contactClass.address.home.country = country;
//                                contactClass.address.home.province = region;
                                contactClass.address.home.city = city;
                                contactClass.address.home.street = street;
                                contactClass.address.home.region = region;
                                contactClass.address.home.postalCode = postCode;
                                contactClass.address.home.formatAddress = formatAddress;
                                break;
                            case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK:
                                contactClass.address.work = new ContactsBean.Address.AddressInfo();
                                contactClass.address.work.country = country;
//                                contactClass.address.work.province = region;
                                contactClass.address.work.city = city;
                                contactClass.address.work.street = street;
                                contactClass.address.work.region = region;
                                contactClass.address.work.postalCode = postCode;
                                contactClass.address.work.formatAddress = formatAddress;
                                break;
                            case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_OTHER:
                                contactClass.address.other = new ContactsBean.Address.AddressInfo();
                                contactClass.address.other.country = country;
//                                contactClass.address.other.province = region;
                                contactClass.address.other.city = city;
                                contactClass.address.other.street = street;
                                contactClass.address.other.region = region;
                                contactClass.address.other.postalCode = postCode;
                                contactClass.address.other.formatAddress = formatAddress;
                                break;
                            default:
                                break;
                        }

                        Log.e(TAG, " country :" + country + " city: " + city + "  street: " + street +
                                "  region: " + region + " postCode: " + postCode + "  formatAddress: " + formatAddress);

                        Log.e(TAG, "addressType : " + addressType);
                    }
                }
                addressCur.close();
                //获取生日
                String[] columns = {ContactsContract.CommonDataKinds.Event.START_DATE,
                        ContactsContract.CommonDataKinds.Event.TYPE,
                        ContactsContract.CommonDataKinds.Event.MIMETYPE};

                String where = ContactsContract.CommonDataKinds.Event.TYPE + "=" + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY +
                        " and " + ContactsContract.CommonDataKinds.Event.MIMETYPE + " = '" + ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE
                        + "' and " + ContactsContract.Data.CONTACT_ID + " = " + id;

                String sortOrder = ContactsContract.Contacts.DISPLAY_NAME;
                Cursor birthdayCur = cr.query(ContactsContract.Data.CONTENT_URI, columns, where, null, sortOrder);

                if (birthdayCur.getCount() > 0) {
                    while (birthdayCur.moveToNext()) {
                        String birthday = birthdayCur.getString(birthdayCur.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
                        int birthdayType = birthdayCur.getInt(birthdayCur.getColumnIndex(ContactsContract.CommonDataKinds.Event.TYPE));
                        Log.e(TAG, "birthday : " + birthday + "  birthdayType: " + birthdayType);
                        contactClass.birthday_g = birthday;
                    }
                    birthdayCur.close();
                }

                // 获取备注信息
                Cursor noteCur = cr.query(ContactsContract.Data.CONTENT_URI,
                        new String[]{ContactsContract.Data._ID, ContactsContract.CommonDataKinds.Note.NOTE},
                        ContactsContract.Data.CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + "='"
                                + ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE + "'", new String[]{id}, null);

                if (noteCur.getCount() > 0) {
                    while (noteCur.moveToNext()) {
                        String note = noteCur.getString(noteCur.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE));
                        Log.e(TAG, "note: " + note);
                        contactClass.remark = note;
                    }
                }
                noteCur.close();

                list.add(contactClass);
            }

            //FastJson, list集合序列化为json格式的字符串
            String s = JSON.toJSONString(list);
            Log.e(TAG, s);
        }
        cur.close();
    }
插入多个手机号码、邮箱、地址等信息
    public static void addContact(Context context, ContactsBean contactsBean) {
        ContentValues values = new ContentValues();

        // 向RawContacts.CONTENT_URI空值插入,
        // 先获取Android系统返回的rawContactId
        // 后面要基于此id插入值
        ContentResolver cr = context.getContentResolver();
        Uri rawContactUri = cr.insert(ContactsContract.RawContacts.CONTENT_URI, values);
        long rawContactId = ContentUris.parseId(rawContactUri);
        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.GIVEN_NAME, contactsBean.name);
        // 向联系人URI添加联系人名字
        cr.insert(ContactsContract.Data.CONTENT_URI, values);
//        values.clear();

        //插入电话号码
        if (contactsBean.phone != null) {
            if (contactsBean.phone.mobile1 != null) {
                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, contactsBean.phone.mobile1);
                // 电话类型
                values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
                cr.insert(ContactsContract.Data.CONTENT_URI, values);
            }
            if (contactsBean.phone.mobile2 != null) {
                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, contactsBean.phone.mobile2);
                // 电话类型
                values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
                cr.insert(ContactsContract.Data.CONTENT_URI, values);
            }
            if (contactsBean.phone.home != null) {
                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, contactsBean.phone.home);
                // 电话类型
                values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_HOME);
                cr.insert(ContactsContract.Data.CONTENT_URI, values);
            }
            if (contactsBean.phone.work != null) {
                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, contactsBean.phone.work);
                // 电话类型
                values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK);
                cr.insert(ContactsContract.Data.CONTENT_URI, values);
            }
            if (contactsBean.phone.faxhome != null) {
                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, contactsBean.phone.faxhome);
                // 电话类型
                values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME);
                cr.insert(ContactsContract.Data.CONTENT_URI, values);
            }
            if (contactsBean.phone.faxwork != null) {
                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, contactsBean.phone.faxwork);
                // 电话类型
                values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK);
                cr.insert(ContactsContract.Data.CONTENT_URI, values);
            }
            if (contactsBean.phone.main != null) {
                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, contactsBean.phone.main);
                // 电话类型
                values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MAIN);
                cr.insert(ContactsContract.Data.CONTENT_URI, values);
            }
            if (contactsBean.phone.other != null) {
                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, contactsBean.phone.other);
                // 电话类型
                values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_OTHER);
                cr.insert(ContactsContract.Data.CONTENT_URI, values);
            }
            if (contactsBean.phone.custom != null) {
                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, contactsBean.phone.custom);
                // 电话类型
                values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM);
                cr.insert(ContactsContract.Data.CONTENT_URI, values);
            }
            if (contactsBean.phone.pager != null) {
                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, contactsBean.phone.pager);
                // 电话类型
                values.put(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_PAGER);
                // 向联系人电话号码URI添加电话号码
                cr.insert(ContactsContract.Data.CONTENT_URI, values);

            }
        }

        //插入地址
        if (contactsBean.address != null) {
            if (contactsBean.address.home != null) {
                values.clear();
                values.put(Data.RAW_CONTACT_ID, rawContactId);
                values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);
                values.put(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME);
                values.put(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, contactsBean.address.home.country);
                values.put(ContactsContract.CommonDataKinds.StructuredPostal.CITY, contactsBean.address.home.city);
                values.put(ContactsContract.CommonDataKinds.StructuredPostal.REGION, contactsBean.address.home.region);
                values.put(ContactsContract.CommonDataKinds.StructuredPostal.STREET, contactsBean.address.home.street);
                values.put(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, contactsBean.address.home.postalCode);
                values.put(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, contactsBean.address.home.formatAddress);
                cr.insert(ContactsContract.Data.CONTENT_URI, values);
            }

            if (contactsBean.address.work != null) {
                values.clear();
                values.put(Data.RAW_CONTACT_ID, rawContactId);
                values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);
                values.put(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK);
                values.put(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, contactsBean.address.work.country);
                values.put(ContactsContract.CommonDataKinds.StructuredPostal.CITY, contactsBean.address.work.city);
                values.put(ContactsContract.CommonDataKinds.StructuredPostal.REGION, contactsBean.address.work.region);
                values.put(ContactsContract.CommonDataKinds.StructuredPostal.STREET, contactsBean.address.work.street);
                values.put(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, contactsBean.address.work.postalCode);
                values.put(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, contactsBean.address.work.formatAddress);
                cr.insert(ContactsContract.Data.CONTENT_URI, values);
            }

            if (contactsBean.address.other != null) {
                values.clear();
                values.put(Data.RAW_CONTACT_ID, rawContactId);
                values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE);
                values.put(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_OTHER);
                values.put(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, contactsBean.address.other.country);
                values.put(ContactsContract.CommonDataKinds.StructuredPostal.CITY, contactsBean.address.other.city);
                values.put(ContactsContract.CommonDataKinds.StructuredPostal.REGION, contactsBean.address.other.region);
                values.put(ContactsContract.CommonDataKinds.StructuredPostal.STREET, contactsBean.address.other.street);
                values.put(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, contactsBean.address.other.postalCode);
                values.put(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, contactsBean.address.other.formatAddress);
                cr.insert(ContactsContract.Data.CONTENT_URI, values);
            }
        }

        //插入邮箱
        if (contactsBean.email != null) {
            if (contactsBean.email.home != null) {
                values.clear();
                values.put(Data.RAW_CONTACT_ID, rawContactId);
                values.put(ContactsContract.Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
                values.put(ContactsContract.CommonDataKinds.Email.DATA, contactsBean.email.home);
                values.put(Email.TYPE, Email.TYPE_HOME);
                cr.insert(ContactsContract.Data.CONTENT_URI, values);
            }

            if (contactsBean.email.work != null) {
                values.clear();
                values.put(Data.RAW_CONTACT_ID, rawContactId);
                values.put(ContactsContract.Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
                values.put(ContactsContract.CommonDataKinds.Email.DATA, contactsBean.email.work);
                values.put(Email.TYPE, Email.TYPE_WORK);
                cr.insert(ContactsContract.Data.CONTENT_URI, values);
            }

            if (contactsBean.email.other != null) {
                values.clear();
                values.put(Data.RAW_CONTACT_ID, rawContactId);
                values.put(ContactsContract.Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
                values.put(ContactsContract.CommonDataKinds.Email.DATA, contactsBean.email.other);
                values.put(Email.TYPE, Email.TYPE_OTHER);
                cr.insert(ContactsContract.Data.CONTENT_URI, values);
            }

            if (contactsBean.email.mobile != null) {
                values.clear();
                values.put(Data.RAW_CONTACT_ID, rawContactId);
                values.put(ContactsContract.Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
                values.put(ContactsContract.CommonDataKinds.Email.DATA, contactsBean.email.mobile);
                values.put(Email.TYPE, Email.TYPE_MOBILE);
                cr.insert(ContactsContract.Data.CONTENT_URI, values);
            }
        }

        //插入生日
        if (contactsBean.birthday_g != null) {
            values.clear();
            values.put(Data.RAW_CONTACT_ID, rawContactId);
            values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE);
            values.put(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY);
            values.put(ContactsContract.CommonDataKinds.Event.START_DATE, contactsBean.birthday_g);
            cr.insert(ContactsContract.Data.CONTENT_URI, values);
        }

        //插入备注
        if (contactsBean.remark != null) {
            values.clear();
            values.put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId);
            values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE);
            values.put(ContactsContract.CommonDataKinds.Note.NOTE, contactsBean.remark);
            cr.insert(ContactsContract.Data.CONTENT_URI, values);
        }
    }

    //插入所有通讯录信息
    public static void insertContact(Context context) {
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                addContact(context, list.get(i));
            }
            list.clear();
        }
    }
ContactsBean, Mode定义
public class ContactsBean implements Serializable {
    public String id;
    public String name;
    public Phone phone;
    public Address address;
    public Email email;

    public static class Phone implements Serializable {
        public String mobile1;
        public String mobile2;
        public String mobile3;
        public String home;
        public String work;
        public String faxhome;
        public String faxwork;
        public String main;
        public String other;
        public String custom;
        public String pager;
    }

    public static class Email implements Serializable {
        public String home;
        public String work;
        public String mobile;
        public String other;
    }

    public static class Address implements Serializable {
        public AddressInfo home;
        public AddressInfo work;
        public AddressInfo other;

        public static class AddressInfo implements Serializable{
            public String country;//国家
            public String province;//省
            public String city;//城市
            public String region;//区
            public String street;//街道
            public String postalCode;//邮编
            public String formatAddress;//完整地址
        }
    }

    public String birthday_g;//公历生日
    public String birthday_l;//农历生日
    public String remark;//备注
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4657449.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-06
下一篇 2022-11-06

发表评论

登录后才能评论

评论列表(0条)

保存