Android访问通讯录和添加联系人

    package com.example.test;  
      
    import java.util.ArrayList;  
      
    import android.content.ContentProviderOperation;  
    import android.content.ContentResolver;  
    import android.content.ContentUris;  
    import android.content.ContentValues;  
    import android.database.Cursor;  
    import android.net.Uri;  
    import android.test.AndroidTestCase;  
    import android.util.Log;  
      
    public class ContactsTest extends AndroidTestCase {  
        private static final String TAG = "ContactsTest";  
      
        /** 
         * 获取联系人 
         * */  
        public void testGetContacts(){  
            Uri uri = Uri.parse("content://com.android.contacts/contacts"); // 访问所有联系人  
            ContentResolver resolver = getContext().getContentResolver();  
            Cursor cursor = resolver.query(uri, new String[]{"_id"}, null, null, null);  
            while(cursor.moveToNext()){  
                int contactsId = cursor.getInt(0);  
                StringBuilder sb = new StringBuilder("contactsId=");  
                sb.append(contactsId);  
                uri = Uri.parse("content://com.android.contacts/contacts/" + contactsId + "/data"); //某个联系人下面的所有数据  
                Cursor dataCursor = resolver.query(uri, new String[]{"mimetype", "data1", "data2"}, null, null, null);  
                while(dataCursor.moveToNext()){  
                    String data = dataCursor.getString(dataCursor.getColumnIndex("data1"));  
                    String type = dataCursor.getString(dataCursor.getColumnIndex("mimetype"));  
                    if("vnd.android.cursor.item/name".equals(type)){    // 如果他的mimetype类型是name  
                        sb.append(", name=" + data);  
                    } else if("vnd.android.cursor.item/email_v2".equals(type)){ // 如果他的mimetype类型是email  
                        sb.append(", email=" + data);  
                    } else if("vnd.android.cursor.item/phone_v2".equals(type)){ // 如果他的mimetype类型是phone  
                        sb.append(", phone=" + data);  
                    }   
                }  
                Log.i(TAG, sb.toString());  
            }  
        }  
          
        /** 
         * 根据来电号码获取联系人名字 
         * */  
        public void testGetContactsByNumber(){  
            String number = "15292328801";  
            Uri uri = Uri.parse("content://com.android.contacts/data/phones/filter/" + number);  
            ContentResolver resolver = getContext().getContentResolver();  
            Cursor cursor = resolver.query(uri, new String[]{"display_name"}, null, null, null);  
            if(cursor.moveToFirst()){  
                String name = cursor.getString(0);  
                Log.i(TAG, name);  
            }  
        }  
          
        /** 
         * 添加联系人 
         * 数据一个表一个表的添加,每次都调用insert方法 
         * */  
        public void testAddContacts(){  
            /* 往 raw_contacts 中添加数据,并获取添加的id号*/  
            Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");  
            ContentResolver resolver = getContext().getContentResolver();  
            ContentValues values = new ContentValues();  
            long contactId = ContentUris.parseId(resolver.insert(uri, values));  
              
            /* 往 data 中添加数据(要根据前面获取的id号) */  
            // 添加姓名  
            uri = Uri.parse("content://com.android.contacts/data");  
            values.put("raw_contact_id", contactId);  
            values.put("mimetype", "vnd.android.cursor.item/name");  
            values.put("data2", "周国平");  
            resolver.insert(uri, values);  
              
            // 添加电话  
            values.clear();  
            values.put("raw_contact_id", contactId);  
            values.put("mimetype", "vnd.android.cursor.item/phone_v2");  
            values.put("data2", "2");  
            values.put("data1", "15099144117");  
            resolver.insert(uri, values);  
              
            // 添加Email  
            values.clear();  
            values.put("raw_contact_id", contactId);  
            values.put("mimetype", "vnd.android.cursor.item/email_v2");  
            values.put("data2", "2");  
            values.put("data1", "zhouguoping@qq.com");  
            resolver.insert(uri, values);  
        }  
          
        /** 
         * 添加联系人 
         * 在同一个事务中完成联系人各项数据的添加 
         * 使用ArrayList<ContentProviderOperation>,把每步操作放在它的对象中执行 
         * */  
        public void testAddContacts2(){  
            Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");  
            ContentResolver resolver = getContext().getContentResolver();  
            // 第一个参数:内容提供者的主机名  
            // 第二个参数:要执行的操作  
            ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();  
              
            // 操作1.添加Google账号,这里值为null,表示不添加  
            ContentProviderOperation operation = ContentProviderOperation.newInsert(uri)  
                    .withValue("account_name", null)// account_name:Google账号  
                    .build();  
              
            // 操作2.添加data表中name字段  
            uri = Uri.parse("content://com.android.contacts/data");  
            ContentProviderOperation operation2 = ContentProviderOperation.newInsert(uri)  
                    // 第二个参数int previousResult:表示上一个操作的位于operations的第0个索引,  
                    // 所以能够将上一个操作返回的raw_contact_id作为该方法的参数  
                    .withValueBackReference("raw_contact_id", 0)  
                    .withValue("mimetype", "vnd.android.cursor.item/name")  
                    .withValue("data2", "周国平")  
                    .build();  
              
            // 操作3.添加data表中phone字段  
            uri = Uri.parse("content://com.android.contacts/data");  
            ContentProviderOperation operation3 = ContentProviderOperation.newInsert(uri)  
                    .withValueBackReference("raw_contact_id", 0)  
                    .withValue("mimetype", "vnd.android.cursor.item/phone_v2")  
                    .withValue("data2", "2")  
                    .withValue("data1", "15099144117")  
                    .build();  
              
            // 操作4.添加data表中的Email字段  
            uri = Uri.parse("content://com.android.contacts/data");  
            ContentProviderOperation operation4 = ContentProviderOperation  
                    .newInsert(uri).withValueBackReference("raw_contact_id", 0)  
                    .withValue("mimetype", "vnd.android.cursor.item/email_v2")  
                    .withValue("data2", "2")  
                    .withValue("data1", "zhouguoping@qq.com").build();  
              
            operations.add(operation);  
            operations.add(operation2);  
            operations.add(operation3);  
            operations.add(operation4);  
              
            try {  
                resolver.applyBatch("com.android.contacts", operations);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }   
        }  
    }  

上一篇:lambda表达式-编译测试


下一篇:Dojo QuickStart 快速入门教程 (4) 简单的测试框架