Android通讯录联系人操作

Android通讯录联系人操作

1. 对Android通讯录进行操作,首先需要加入读写联系人信息的权限:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
2. 读取联系人信息:
public void testGetAllContact() throws Exception {
        //Uri uri = Uri.parse("content://com.android.contacts/contacts");
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        ContentResolver contentResolver = this.getContext().getContentResolver();
        Cursor cursor = contentResolver.query(uri, null, null, null, null);
        while(cursor.moveToNext()) {
            StringBuilder sb = new StringBuilder();
            String contactId = cursor.getString(cursor.getColumnIndex(Contacts._ID));
            String name = cursor.getString(cursor.getColumnIndex(Contacts.DISPLAY_NAME));
            sb.append("contactId=").append(contactId).append(", name=").append(name);

            Cursor phones = contentResolver.query(
                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + contactId,
                    null, null);
            while(phones.moveToNext()) {
                String phoneNumber = phones.getString(phones.getColumnIndex(
                        ContactsContract.CommonDataKinds.Phone.NUMBER));
                sb.append(", phoneNumber=").append(phoneNumber);
            }
            phones.close();

            Cursor emails = contentResolver.query(
                    ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
                    ContactsContract.CommonDataKinds.Email.CONTACT_ID + "=" + contactId,
                    null, null);
            while(emails.moveToNext()) {
                String emailAddress = emails.getString(emails.getColumnIndex(
                        ContactsContract.CommonDataKinds.Email.DATA));
                sb.append(", emailAddress=").append(emailAddress);
            }
            emails.close();
            Log.i("CONTACT", sb.toString());
        }
        cursor.close();
    }
3. 新建联系人,第一种方法,逐个数据进行添加:
public void testInsert() throws Exception {
        ContentValues values = new ContentValues();
        Uri rawContactUri = this.getContext().getContentResolver().
                insert(RawContacts.CONTENT_URI, values);
        long rawContactId = ContentUris.parseId(rawContactUri);

        values.clear();
        values.put(Data.RAW_CONTACT_ID, rawContactId);
        values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE);
        values.put(StructuredName.GIVEN_NAME, "Garing");
        this.getContext().getContentResolver().insert(
                android.provider.ContactsContract.Data.CONTENT_URI, values);

        values.clear();
        values.put(Data.RAW_CONTACT_ID, rawContactId);
        values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
        values.put(Phone.NUMBER, "13760188958");
        values.put(Phone.TYPE, Phone.TYPE_MOBILE);
        this.getContext().getContentResolver().insert(
                android.provider.ContactsContract.Data.CONTENT_URI, values);

        values.clear();
        values.put(Data.RAW_CONTACT_ID, rawContactId);
        values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
        values.put(Email.DATA, "hahha@hotmail.com");
        values.put(Email.TYPE, Email.TYPE_WORK);
        this.getContext().getContentResolver().insert(
                android.provider.ContactsContract.Data.CONTENT_URI, values);
    }
4. 新建联系人,第二种方法,官方,将添加的联系人的所有信息(电话,电邮)放在同一事务中进行添加操作:
public void testSave() throws Exception {
        ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
        int rawContactInsertIndex = ops.size();
        ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
                .withValue(RawContacts.ACCOUNT_TYPE, null)
                .withValue(RawContacts.ACCOUNT_NAME, null)
                .build());

        ops.add(ContentProviderOperation.newInsert(android.provider.ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
                .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
                .withValue(StructuredName.GIVEN_NAME, "奥巴马")
                .build());

        ops.add(ContentProviderOperation.newInsert(android.provider.ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
                .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
                .withValue(Phone.NUMBER, "13982877689")
                .withValue(Phone.TYPE, Phone.TYPE_MOBILE)
                .withValue(Phone.LABEL, "手机号")
                .build());

        ops.add(ContentProviderOperation.newInsert(android.provider.ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
                .withValue(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)
                .withValue(Email.DATA, "13982877689@163.com")
                .withValue(Email.TYPE, Email.TYPE_WORK)
                .build());

        ContentProviderResult[] results = this.getContext().getContentResolver()
                .applyBatch(ContactsContract.AUTHORITY, ops);
        for(ContentProviderResult result : results) {
            Log.i("RESULT", result.uri.toString());
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值