获取联系人
- 主要数据库表: Data 联系人数据
raw_contacts 表 这个表存储的联系人信息
mimetypes 表 这个表存储的数据类型
具体含义一目了然!!!!!!! 下面上代码!!!!:
ContentResolver resolver = context.getContentResolver();
AllContactAsyncQuery asyncquery = new AllContactAsyncQuery(resolver);
asyncquery.startQuery(0, null, ContactsContract.Contacts.CONTENT_URI,
null, null, null, ContactsContract.Contacts.SORT_KEY_PRIMARY
+ " COLLATE LOCALIZED ASC");
!!! 这里查询的表 是 contacts 表 这个表里面有photo_id 因为我所做项目需要这个所以查询的这个表!!
@Override
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
ArrayList<ContactsModel> list = new ArrayList<ContactsModel>();
if (cursor == null) {
return;
}
if (cursor.moveToFirst()) {
do {
ContactsModel model = new ContactsModel();
// 获得联系人的ID号
String contactId = cursor.getLong(cursor
.getColumnIndex(ContactsContract.Contacts._ID))
+ "";
String raw_contactId = cursor.getLong(cursor
.getColumnIndex("name_raw_contact_id")) + "";
String namepinyin = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.SORT_KEY_PRIMARY));
if(namepinyin!=null){
namepinyin = Contast.DeleteCha(namepinyin);
}
int times = cursor
.getInt(cursor
.getColumnIndex(ContactsContract.Contacts.TIMES_CONTACTED));
int picid = cursor
.getInt(cursor
.getColumnIndex(ContactsContract.Contacts.PHOTO_ID));
// //打电话次数
// 查看该联系人有多少个电话号码。如果没有这返回值为0
int phoneCount = cursor
.getInt(cursor
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String number = null;
byte[] picpath = null;
String email = null;
if (phoneCount > 0) {
// 获得联系人的电话号码
Cursor phones = context
.getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null, null);
if (phones != null) {
if (phones.moveToFirst()) {
number = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
// 获取图片路径
Uri photoUri = ContentUris.withAppendedId(
ContactsContract.Data.CONTENT_URI, picid);
phones = context
.getContentResolver()
.query(photoUri,
new String[] { ContactsContract.CommonDataKinds.Photo.PHOTO },
null, null, null);
if (phones.moveToFirst()) {
picpath = phones.getBlob(0);
}
phones.close();
// 获取email
phones = context
.getContentResolver()
.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId, null, null);
if (phones.moveToFirst()) {
email = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
}
phones.close();
phones = context
.getContentResolver()
.query(ContactsContract.RawContacts.CONTENT_URI,
null,"_id = " + raw_contactId, null, null);
// 获得联系人姓名
String name = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
phones.close();
model.setContactID(raw_contactId);
model.setName(name);
model.setPhoneNum1(number);
model.setNamepinyin(namepinyin);
model.setTimes(times);
model.setPic(picpath);
model.setEmail(email);
list.add(model);
} else {
}
} while (cursor.moveToNext());
cursor.close();
super.onQueryComplete(token, cookie, cursor);
}
其余插入更新删除大同小异!!!