一样,因为这是对通讯录进行操作,因此我们需要添加相应的权限。
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
代码如下所示:
package com.itfom.contentprovider; import android.app.Activity; import android.content.ContentResolver; import android.content.ContentUris; import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.provider.ContactsContract.CommonDataKinds.Email; import android.provider.ContactsContract.CommonDataKinds.Phone; import android.provider.ContactsContract.CommonDataKinds.StructuredName; import android.provider.ContactsContract.Data; import android.provider.ContactsContract.RawContacts; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { private static final String TAG = "ContactTest"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //实例化控件并绑定监听事件 Button btn_get=(Button) findViewById(R.id.btn_get); Button btn_add=(Button) findViewById(R.id.btn_add); //设置监听事件 //添加联系人 btn_add.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ContentValues values = new ContentValues(); //首先向RawContacts.CONTENT_URI执行一个空值插入,目的是获取系统返回的rawContactId Uri rawContactUri = MainActivity.this.getContentResolver().insert(RawContacts.CONTENT_URI, values); long rawContactId = ContentUris.parseId(rawContactUri); //往data表入姓名数据 values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE); values.put(StructuredName.GIVEN_NAME, "zhangsan"); MainActivity.this.getContentResolver().insert( android.provider.ContactsContract.Data.CONTENT_URI, values); //往data表入电话数据 values.clear(); values.put(android.provider.ContactsContract.Contacts.Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); values.put(Phone.NUMBER, "5554"); values.put(Phone.TYPE, Phone.TYPE_MOBILE); MainActivity.this.getContentResolver().insert( android.provider.ContactsContract.Data.CONTENT_URI, values); //往data表入Email数据 values.clear(); values.put(android.provider.ContactsContract.Contacts.Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE); values.put(Email.DATA, "[email protected]"); values.put(Email.TYPE, Email.TYPE_WORK); MainActivity.this.getContentResolver().insert( android.provider.ContactsContract.Data.CONTENT_URI, values); } }); //获取联系人 btn_get.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ContentResolver contentResolver = MainActivity.this.getContentResolver(); Uri uri = Uri.parse("content://com.android.contacts/contacts"); Cursor cursor = contentResolver.query(uri, null, null, null, null); while(cursor.moveToNext()){ // 获取联系人姓名 StringBuilder sb = new StringBuilder(); String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); String name = cursor.getString(cursor.getColumnIndex(ContactsContract.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 phone = phones.getString(phones.getColumnIndex("data1")); sb.append(",phone=").append(phone); } //获取联系人email Cursor emails = contentResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null); while(emails.moveToNext()){ String email = emails.getString(emails.getColumnIndex("data1")); sb.append(",email=").append(email); } Log.i(TAG, sb.toString()); } } }); } }