Android 联系人添加

/**
     * @brief 新建联系人显示编辑界面
     * */
    public void insertContact(Contact contact,Context context) {

        if (context == null) {
            return;
        }

        Intent intent = new Intent(ContactsContract.Intents.Insert.ACTION);
        intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);

        intent.putExtra(ContactsContract.Intents.Insert.NAME, contact.getName());

        intent.putExtra(ContactsContract.Intents.Insert.PHONE, contact.getNumber());
        intent.putExtra(ContactsContract.Intents.Insert.PHONE_TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK);

        intent.putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE, contact.getFax());
        intent.putExtra(ContactsContract.Intents.Insert.SECONDARY_PHONE_TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK);

        intent.putExtra(ContactsContract.Intents.Insert.EMAIL, contact.getEmail());
        intent.putExtra(ContactsContract.Intents.Insert.EMAIL_TYPE, ContactsContract.CommonDataKinds.Email.TYPE_WORK);

        intent.putExtra(ContactsContract.Intents.Insert.POSTAL, contact.getAddress());
        intent.putExtra(ContactsContract.Intents.Insert.POSTAL_TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK);

        // Insert中没有的类型,通过Data写入
        ArrayList<ContentValues> data = new ArrayList<ContentValues>();

        ContentValues row1 = new ContentValues();
        row1.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE);
        row1.put(ContactsContract.CommonDataKinds.Website.DATA, contact.getHomePage());
        data.add(row1);

        ContentValues row2 = new ContentValues();
        row2.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE);
        row2.put(ContactsContract.CommonDataKinds.Photo.PHOTO, contact.getPhotoData());
        data.add(row2);

//        Intent intent = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);

        intent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA, data);

        context.startActivity(intent);

    }

    /**
     * 添加联系人,但是不显示编辑界面。
     * */
    private void addContact(Contact contact) {

        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();

        ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
                .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
                .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
                .build());
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(COLUMN_RAW_CONTACT_ID, 0)
                .withValue(COLUMN_MIMETYPE, MIMETYPE_STRING_NAME)
                .withValue(COLUMN_NAME, contact.getName())
                .build());
        Log.d(TAG, "add name: " + contact.getName());

        // photo
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(COLUMN_RAW_CONTACT_ID, 0)
                .withValue(COLUMN_MIMETYPE, MIMETYPE_STRING_PHOTO)
                .withValue(COLUMN_PHOTO, contact.getPhotoData())
                .build());

        // web
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(COLUMN_RAW_CONTACT_ID, 0)
                .withValue(COLUMN_MIMETYPE, MIMETYPE_STRING_WEB)
                .withValue(COLUMN_WEB,contact.getHomePage())
                .withValue(COLUMN_WEB_TYPE, contact.getHomePage())
                .build());

        // postal
        ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(COLUMN_RAW_CONTACT_ID, 0)
                .withValue(COLUMN_MIMETYPE, MIMETYPE_STRING_ADDRESS)
                .withValue(COLUMN_POSTAL,contact.getAddress())
                .withValue(COLUMN_POSTAL_TYPE, contact.getAddressType())
                .build());

        if(!contact.getNumber().trim().equals("")) {
            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(COLUMN_RAW_CONTACT_ID, 0)
                    .withValue(COLUMN_MIMETYPE, MIMETYPE_STRING_PHONE)
                    .withValue(COLUMN_NUMBER, contact.getNumber())
                    .withValue(COLUMN_NUMBER_TYPE, contact.getNumberType())
                    .build());
            Log.d(TAG, "add number: " + contact.getNumber());
        }

        if(!contact.getFax().trim().equals("")) {
            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(COLUMN_RAW_CONTACT_ID, 0)
                    .withValue(COLUMN_MIMETYPE, MIMETYPE_STRING_PHONE)
                    .withValue(COLUMN_NUMBER, contact.getFax())
                    .withValue(COLUMN_NUMBER_TYPE, contact.getFaxType())
                    .build());
            Log.d(TAG, "add fax: " + contact.getFax());
        }

        if(!contact.getEmail().trim().equals("")) {
            ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(COLUMN_RAW_CONTACT_ID, 0)
                    .withValue(COLUMN_MIMETYPE, MIMETYPE_STRING_EMAIL)
                    .withValue(COLUMN_EMAIL, contact.getEmail())
                    .withValue(COLUMN_EMAIL_TYPE, contact.getEmailType())
                    .build());
            Log.d(TAG, "add email: " + contact.getEmail());
        }

        try {
            contentResolver.applyBatch(ContactsContract.AUTHORITY, ops);
            Log.d(TAG, "add contact success.");
        } catch (Exception e) {
            Log.d(TAG, "add contact failed.");
            Log.e(TAG, e.getMessage());
        }
    }

 

posted @ 2018-04-28 10:08  Emerys  阅读(306)  评论(0编辑  收藏  举报