直接上代码:
管理通讯录的类:ManagerContact.java
package com.example.contactdemo; import java.util.ArrayList; import android.content.ContentProviderOperation; import android.content.ContentResolver; import android.content.ContentUris; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.provider.ContactsContract; import android.provider.ContactsContract.CommonDataKinds.Email; import android.provider.ContactsContract.CommonDataKinds.Note; import android.provider.ContactsContract.CommonDataKinds.Organization; import android.provider.ContactsContract.CommonDataKinds.Phone; import android.provider.ContactsContract.CommonDataKinds.StructuredName; import android.provider.ContactsContract.CommonDataKinds.Website; import android.provider.ContactsContract.Data; import android.provider.ContactsContract.RawContacts; import android.util.Log; /** * 管理通讯录的类 * @author Willen * */ public class ManagerContact { // 根据姓名删除联系人 public void delContact(Context context, String name) { Cursor cursor = context.getContentResolver().query(Data.CONTENT_URI, new String[] { Data.RAW_CONTACT_ID }, ContactsContract.Contacts.DISPLAY_NAME + "=?", new String[] { name }, null); ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); if (cursor.moveToFirst()) { do { long Id = cursor.getLong(cursor .getColumnIndex(Data.RAW_CONTACT_ID)); ops.add(ContentProviderOperation .newDelete( ContentUris.withAppendedId( RawContacts.CONTENT_URI, Id)).build()); try { context.getContentResolver().applyBatch( ContactsContract.AUTHORITY, ops); } catch (Exception e) { } } while (cursor.moveToNext()); cursor.close(); } } // 根据号码删除联系人 public void delContactByNumber(Context context, String number) { Cursor cursor = context.getContentResolver().query(Data.CONTENT_URI, new String[] { Data.RAW_CONTACT_ID }, ContactsContract.Contacts.Data.DATA1 + "=?", new String[] { number }, null); ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); if (cursor.moveToFirst()) { do { long Id = cursor.getLong(cursor .getColumnIndex(Data.RAW_CONTACT_ID)); ops.add(ContentProviderOperation .newDelete( ContentUris.withAppendedId( RawContacts.CONTENT_URI, Id)).build()); try { context.getContentResolver().applyBatch( ContactsContract.AUTHORITY, ops); } catch (Exception e) { } } while (cursor.moveToNext()); cursor.close(); } } // 根据姓名更新联系人 public boolean updateContact(Context context, String oldname, String name, String phone, String email, String website, String organization, String note) { Cursor cursor = context.getContentResolver().query(Data.CONTENT_URI, new String[] { Data.RAW_CONTACT_ID }, ContactsContract.Contacts.DISPLAY_NAME + "=?", new String[] { oldname }, null); if (!cursor.moveToFirst()) { return false; } String id = cursor .getString(cursor.getColumnIndex(Data.RAW_CONTACT_ID)); cursor.close(); ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); ops.add(ContentProviderOperation .newUpdate(ContactsContract.Data.CONTENT_URI) .withSelection( Data.RAW_CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + " = ?" + " AND " + Phone.TYPE + "=?", new String[] { String.valueOf(id), Phone.CONTENT_ITEM_TYPE, String.valueOf(Phone.TYPE_HOME) }) .withValue(Phone.NUMBER, phone).build()); // 更新email ops.add(ContentProviderOperation .newUpdate(ContactsContract.Data.CONTENT_URI) .withSelection( Data.RAW_CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + " = ?" + " AND " + Email.TYPE + "=?", new String[] { String.valueOf(id), Email.CONTENT_ITEM_TYPE, String.valueOf(Email.TYPE_HOME) }) .withValue(Email.DATA, email).build()); // 更新姓名 ops.add(ContentProviderOperation .newUpdate(ContactsContract.Data.CONTENT_URI) .withSelection( Data.RAW_CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + " = ?", new String[] { String.valueOf(id), StructuredName.CONTENT_ITEM_TYPE }) .withValue(StructuredName.DISPLAY_NAME, name).build()); // 更新网站 ops.add(ContentProviderOperation .newUpdate(ContactsContract.Data.CONTENT_URI) .withSelection( Data.RAW_CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + " = ?", new String[] { String.valueOf(id), Website.CONTENT_ITEM_TYPE }) .withValue(Website.URL, website).build()); // 更新公司 ops.add(ContentProviderOperation .newUpdate(ContactsContract.Data.CONTENT_URI) .withSelection( Data.RAW_CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + " = ?", new String[] { String.valueOf(id), Organization.CONTENT_ITEM_TYPE }) .withValue(Organization.COMPANY, organization).build()); // 更新note ops.add(ContentProviderOperation .newUpdate(ContactsContract.Data.CONTENT_URI) .withSelection( Data.RAW_CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + " = ?", new String[] { String.valueOf(id), Note.CONTENT_ITEM_TYPE }) .withValue(Note.NOTE, note).build()); try { context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); return true; } catch (Exception e) { return false; } } // 根据号码更新联系人 public boolean updateContactByNumber(Context context, String number, String name, String phone, String email, String website, String organization, String note) { Cursor cursor = context.getContentResolver().query(Data.CONTENT_URI, new String[] { Data.RAW_CONTACT_ID }, ContactsContract.Contacts.Data.DATA1 + "=?", new String[] { number }, null); if (!cursor.moveToFirst()) { Log.e("InsertContact", "updateContactByNumber"); InsertContact(context, name, number); return false; } String id = cursor .getString(cursor.getColumnIndex(Data.RAW_CONTACT_ID)); cursor.close(); ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); ops.add(ContentProviderOperation .newUpdate(ContactsContract.Data.CONTENT_URI) .withSelection( Data.RAW_CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + " = ?" + " AND " + Phone.TYPE + "=?", new String[] { String.valueOf(id), Phone.CONTENT_ITEM_TYPE, String.valueOf(Phone.TYPE_HOME) }) .withValue(Phone.NUMBER, phone).build()); // 更新email ops.add(ContentProviderOperation .newUpdate(ContactsContract.Data.CONTENT_URI) .withSelection( Data.RAW_CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + " = ?" + " AND " + Email.TYPE + "=?", new String[] { String.valueOf(id), Email.CONTENT_ITEM_TYPE, String.valueOf(Email.TYPE_HOME) }) .withValue(Email.DATA, email).build()); // 更新姓名 ops.add(ContentProviderOperation .newUpdate(ContactsContract.Data.CONTENT_URI) .withSelection( Data.RAW_CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + " = ?", new String[] { String.valueOf(id), StructuredName.CONTENT_ITEM_TYPE }) .withValue(StructuredName.DISPLAY_NAME, name).build()); // 更新网站 ops.add(ContentProviderOperation .newUpdate(ContactsContract.Data.CONTENT_URI) .withSelection( Data.RAW_CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + " = ?", new String[] { String.valueOf(id), Website.CONTENT_ITEM_TYPE }) .withValue(Website.URL, website).build()); // 更新公司 ops.add(ContentProviderOperation .newUpdate(ContactsContract.Data.CONTENT_URI) .withSelection( Data.RAW_CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + " = ?", new String[] { String.valueOf(id), Organization.CONTENT_ITEM_TYPE }) .withValue(Organization.COMPANY, organization).build()); // 更新note ops.add(ContentProviderOperation .newUpdate(ContactsContract.Data.CONTENT_URI) .withSelection( Data.RAW_CONTACT_ID + "=?" + " AND " + ContactsContract.Data.MIMETYPE + " = ?", new String[] { String.valueOf(id), Note.CONTENT_ITEM_TYPE }) .withValue(Note.NOTE, note).build()); try { context.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); return true; } catch (Exception e) { return false; } } // 添加联系人 public void InsertContact(Context context, String name, String phone) { ContentResolver resolver = context.getContentResolver(); Uri uri = Uri.parse("content://com.android.contacts/raw_contacts"); ContentValues values = new ContentValues(); // 向raw_contacts插入一条除了ID之外, 其他全部为NULL的记录, ID是自动生成的 long id = ContentUris.parseId(resolver.insert(uri, values)); // 添加联系人姓名 uri = Uri.parse("content://com.android.contacts/data"); values.put("raw_contact_id", id); values.put("data2", name); values.put("mimetype", "vnd.android.cursor.item/name"); resolver.insert(uri, values); // 添加联系人电话 values.clear(); // 清空上次的数据 values.put("raw_contact_id", id); values.put("data1", phone); values.put("data2", "2"); values.put("mimetype", "vnd.android.cursor.item/phone_v2"); resolver.insert(uri, values); } //判断联系人是否在通讯录内 public boolean getContactPeople(Context context,String incomingNumber) { ContentResolver contentResolver = context.getContentResolver(); Cursor cursor = null; String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER }; cursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, ContactsContract.CommonDataKinds.Phone.NUMBER + "=?", new String[] { incomingNumber }, ""); if(cursor.getCount() == 0) { return true; } return false; } }
MainActivity.java
package com.example.contactdemo; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; public class MainActivity extends Activity { Context context; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); context = this; findViewById(R.id.button1).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { ManagerContact managerContact = new ManagerContact(); String phone = "15992470000"; if (!managerContact.getContactPeople(context, phone)) { Toast.makeText(context, "本地通讯录已包此人信息", Toast.LENGTH_SHORT).show(); }else { managerContact.InsertContact(context, "aa", phone); Toast.makeText(context, "添加成功!", Toast.LENGTH_SHORT).show(); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
布局文件只有一个button。资源:http://download.csdn.net/detail/viviwen123/5194623