Android address book saves information such as mobile phone, telephone, email, address, etc.

show yourself the series

storage tools

  • tips: Before storing in the phone address book, you need to obtain the runtime permission of the phone address book

Create the ContentProviderOperationentity , fill in the corresponding information and required fields ID, add it to the ArrayList, and then use ContentResolverthe applyBatchmethod for batch operations.

public static void InsertContact(Context context, ContactBean contact) {
    ContentValues values = new ContentValues();

    Uri rawContactUri = Uri.parse("content://com.android.contacts/raw_contacts");
    ContentResolver resolver = context.getContentResolver();

    // 生成 id
    long rawContactId = ContentUris.parseId(resolver.insert(rawContactUri, values));

    // 插入数据
    rawContactUri = Uri.parse("content://com.android.contacts/data");
    ArrayList<ContentProviderOperation> ops = new ArrayList<>();
    // 写入名字
    if (null != contact.getName() && !"".equals(contact.getName())) {
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
                .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, contact.getName())
                .build());
    }

    // 写入手机号
    if (null != contact.getMobile()) {
        for (MobileBean mobile : contact.getMobile()) {
            if (null != mobile.getContent() && !"".equals(mobile.getContent())) {
                ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                        .withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
                        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, mobile.getContent())
                        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
                        .build());
            }
        }
    }
    // 座机
    if (null != contact.getPhone()) {
        for (PhoneBean phone : contact.getPhone()) {
            if (null != phone.getContent() && !"".equals(phone.getContent())) {
                ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                        .withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
                        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                        .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, phone.getContent())
                        .withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK)
                        .build());
            }
        }
    }

    // 邮箱
    if (null != contact.getEmail()) {
        for (EmailBean email : contact.getEmail()) {
            if (null != email.getContent() && !"".equals(email.getContent())) {
                ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                        .withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
                        .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
                        .withValue(ContactsContract.CommonDataKinds.Email.ADDRESS, email.getContent())
                        .withValue(ContactsContract.CommonDataKinds.Email.TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK)
                        .build());
            }
        }
    }

    // 公司
    if (null != contact.getCompany()) {
        for (CompanyBean company : contact.getCompany()) {
            ContentProviderOperation.Builder builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
                    .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
                    .withValue(ContactsContract.CommonDataKinds.Organization.TYPE, ContactsContract.CommonDataKinds.Organization.TYPE_WORK);
            if (null != company.getComname() && !"".equals(company.getComname())) {
                builder.withValue(ContactsContract.CommonDataKinds.Organization.COMPANY, company.getComname());
            }
            if (null != company.getZhiwu() && !"".equals(company.getZhiwu())) {
                builder.withValue(ContactsContract.CommonDataKinds.Organization.DEPARTMENT, company.getZhiwu());
                builder.withValue(ContactsContract.CommonDataKinds.Organization.OFFICE_LOCATION, company.getZhiwu());
                builder.withValue(ContactsContract.CommonDataKinds.Organization.JOB_DESCRIPTION, company.getZhiwu());
                builder.withValue(ContactsContract.CommonDataKinds.Organization.PHONETIC_NAME, company.getZhiwu());
                builder.withValue(ContactsContract.CommonDataKinds.Organization.TITLE, company.getZhiwu());
            }

            ops.add(builder.build());
        }
    }

    // 地址
    if (null != contact.getAddress() && !"".equals(contact.getAddress())) {
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValue(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
                .withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK)
                .withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, contact.getAddress())
                .build());
    }


    try {
        resolver.applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (OperationApplicationException | RemoteException e) {
        e.printStackTrace();
    }
}
复制代码

Mobile phone number verification tools

/**
 * 判断某个手机号是否存在
 */
private boolean isPhoneExist(Context context, String phoneNum) {
    Cursor cursor = null;
    Uri uri = Uri.parse("content://com.android.contacts/data/phones/filter/" + phoneNum);
    ContentResolver resolver = context.getContentResolver();
    cursor = resolver.query(uri, new String[]{ContactsContract.Data.DISPLAY_NAME},
            null, null, null);
    if (cursor.moveToFirst()) {
        cursor.close();
        return true;
    }

    cursor.close();
    return false;
}
复制代码

Guess you like

Origin juejin.im/post/7078589419254448164