Show yourself a series

Storage utility class

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

Create the ContentProviderOperation entity that you want to store the information to, fill it in with the information and the required ID, add it to the ArrayList, and use the applyBatch method of the ContentResolver to batch it.

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();

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

    // Insert data
    rawContactUri = Uri.parse("content://com.android.contacts/data");
    ArrayList<ContentProviderOperation> ops = new ArrayList<>();
    // Write the name
    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());
    }

    // Write the phone number
    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()); }}}/ / machine
    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()); }}}/ / email
    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()); }}}/ / the company
    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()); }}/ / address
    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(); }}Copy the code

Mobile phone number weight testing tools

/** * Check whether a mobile phone number exists */
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;
}
Copy the code