/*********删除通用******************************************************************/
ContentResolver contentResolver = AddContactLog.this.getContentResolver(); contentResolver.delete(Contacts.People.CONTENT_URI, Contacts.People.NAME + "=?", new String[] { "名字" });
/*********before 2.0******************************************************************/
ContentValues values = new ContentValues(); Uri phoneUri = null; values.put(Contacts.People.NAME, "名字"); values.put(Contacts.People.STARRED, 1); Uri uri = Contacts.People.createPersonInMyContactsGroup(contentResolver, values); phoneUri = Uri.withAppendedPath(uri, Contacts.People.Phones.CONTENT_DIRECTORY); values.clear(); values.put(Contacts.Phones.TYPE, Contacts.Phones.NUMBER); System.out.println("---------------------------------------"); values.put(Contacts.Phones.NUMBER, serviceNumber); contentResolver.insert(phoneUri, values);
/*********after 2.0******************************************************************/
// 获取通讯录中所有联系人 public void testGetContact() { StringBuffer sb = new StringBuffer(); ContentResolver contentResolver = this.getContext() .getContentResolver(); Uri uri = Uri.parse("content://com.android.contacts/contacts"); Cursor cursor = contentResolver.query(uri, null, null, null, null); while (cursor.moveToNext()) { 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(ContactsContract.CommonDataKinds.Phone.NUMBER)); sb.append(",phone=").append(phone); } 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(phones .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); sb.append(",email=").append(email); } Log.i(TAG, sb.toString()); } }
/**************************************************************************/
/** * 首先想RawContacts.CONTENT URI 执行一个空值插入,目的是为了获取返回的rawContactId * 这是后面插入data表的依据,只有执行空值插入,才能使插入的联系人在通讯录里面可见 */ public void testInsert() { ContentValues values = new ContentValues(); // 首先想RawContacts.CONTENT_URI执行一个空值插入,目的似乎或偶去系统返回的rawContactId Uri rawContactUri = this.getContext().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, "小样"); this.getContext().getContentResolver().insert( android.provider.ContactsContract.Data.CONTENT_URI, values); // 往data表入电话数据 values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); values.put(Phone.NUMBER, "12345678901"); values.put(Phone.TYPE, Phone.TYPE_MOBILE); this.getContext().getContentResolver().insert( android.provider.ContactsContract.Data.CONTENT_URI, values); // 往data表入Email数据 values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE); values.put(Email.DATA, "[email protected]"); values.put(Email.TYPE, Phone.TYPE_WORK); this.getContext().getContentResolver().insert( android.provider.ContactsContract.Data.CONTENT_URI, values); }