实例教程十一:访问通信录中的联系人和添加联系人

Android《五子连珠》游戏完整源码分享

http://www.eoeandroid.com/thread-211877-1-1.html 

android仿酷狗播放器

http://www.eoeandroid.com/thread-185894-1-1.html

自制K线图,新鲜出炉

http://www.eoeandroid.com/thread-182290-1-1.html

package cn.itcast.contacts;

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";

        /**
         * 获取 所有联系人
         * @throws Exception
         */
        public void testContacts() throws Exception{
                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 contactId = cursor.getInt(0);
                        StringBuilder sBuilder = new StringBuilder("contactId=");
                        sBuilder.append(contactId);

                        uri = Uri.parse("content://com.android.contacts/contacts" + contactId + "/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("data2"));
                                if("vnd.android.cursor.item/name".equals(type)){ //姓名
                                        sBuilder.append(", name=" + data);
                                }else if("vnd.android.cursor.item/email_v2".equals(type)){ //email
                                        sBuilder.append(", email=" + data);
                                }else if("vnd.android.cursor.item/phone_v2".equals(type)){ //phone
                                        sBuilder.append(", phone=" + data);
                                }
                        }
                        Log.i(TAG, sBuilder.toString());
                }
        }

        /**
         * 根据号码获取联系人的姓名
         * @throws Exception
         */
        public void testContactNameByPhoneNumber() throws Exception{
                String pNumber = "18012345678";
                Uri uri = Uri.parse("content://com.android.contacts/data/phones/filter/" + pNumber);
                ContentResolver resolver = getContext().getContentResolver();
                Cursor cursor = resolver.query(uri, new String[]{"display_name"}, null, null, null);
                //为何使用moveToFirst()?
                //因为查找的数据只有一条,那一开始肯定就有一条
                if(cursor.moveToFirst()){
                        String name = cursor.getString(0);
                        Log.i(TAG, name);
                }
                cursor.close();
        }

        /**
         * 添加联系人
         * @throws Exception
         */
        public void testAddContact() throws Exception{
                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));
                //添加姓名
                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", "13671325874");
                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", "[email protected]");
                resolver.insert(uri, values);
        }

        /**
         * 在同一事务中完成 联系人各项数据的添加
         * @throws Exception
         */
        public void testAddContact2() throws Exception{
                Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
                ContentResolver resolver = getContext().getContentResolver();
                ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
                ContentProviderOperation op1 = ContentProviderOperation.newInsert(uri)
                .withValue("account_name", null)
                .build();                
                operations.add(op1);

                uri = Uri.parse("content://com.android.contacts/data");
                //添加姓名
                //0--使用第一操作对象 添加完记录后所返回的ID
                ContentProviderOperation op2 = ContentProviderOperation.newInsert(uri)
                .withValueBackReference("raw_contact_id", 0)
                .withValue("mimetype", "vnd.android.cursor.item/name")
                .withValue("data2", "张三")
                .build();                
                operations.add(op2);

                ContentProviderOperation op3 = ContentProviderOperation.newInsert(uri)
                .withValueBackReference("raw_contact_id", 0)
                .withValue("mimetype", "vnd.android.cursor.item/phone_v2")
                .withValue("data1", "13671325874")
                .withValue("data2", "2")                
                .build();                
                operations.add(op3);
                
                ContentProviderOperation op4 = ContentProviderOperation.newInsert(uri)
                .withValueBackReference("raw_contact_id", 0)
                .withValue("mimetype", "vnd.android.cursor.item/email_v2")
                .withValue("data1", "[email protected]")
                .withValue("data2", "2")                
                .build();                
                operations.add(op4);

                resolver.applyBatch("com.android.contacts", operations);
                //applyBatch运行流程
                //{
                //        begin();
                //        for(op: operations){
                //                op.app()
                //        }
                //        end();
                //}
        }
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="cn.itcast.contacts"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <uses-library android:name="android.test.runner" />
    </application>

    <instrumentation
        android:name="android.test.InstrumentationTestRunner"
        android:label="Tests for My App"
        android:targetPackage="cn.itcast.contacts" >
    </instrumentation>
    
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.WRITE_CONTACTS"/>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>

</manifest>

 

你可能感兴趣的:(联系人)