首页 > 代码库 > (android)解剖contentProvider获取通讯录列表-002
(android)解剖contentProvider获取通讯录列表-002
//基于上一次说的内容 ,我们来弄下获取联系人详细信息,直接上代码,在第三篇补上其他的操作
package org.mo.pmas.resolver; //本文章归属于小黑_mo所有,转载请说明出来http://my.oschina.net/moziqi/ import android.content.ContentResolver; import android.content.ContentUris; import android.content.Context; import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.provider.ContactsContract; import android.util.Log; import org.mo.pmas.activity.R; import org.mo.pmas.entity.Contact; import org.mo.pmas.entity.Phone; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; /** * Created by moziqi on 2015/1/9 0009. */ public class ContactResolver implements BaseResolver<Contact> { private Context mContext; public ContactResolver(Context mContext) { this.mContext = mContext; } @Override public boolean save(Contact entity) { return false; } @Override public boolean update(Contact entity) { return false; } @Override public boolean delete(Contact entity) { return false; } @Override public List<Contact> findAll() { List<Contact> contacts = new ArrayList<Contact>(); ContentResolver contentResolver = mContext.getContentResolver(); Cursor cursor = contentResolver.query( ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"); if (cursor.moveToFirst()) { do { Contact contact = new Contact(); // 获得联系人的ID号 int contactId = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts._ID)); // 获得联系人姓名 String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); //获取联系人生日 String[] projection = new String[]{ContactsContract.CommonDataKinds.Event.DATA1}; String selection = ContactsContract.Data.MIMETYPE + "=‘" + ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE + "‘" + " and " + ContactsContract.CommonDataKinds.Event.TYPE + "=‘" + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY + "‘" + " and " + ContactsContract.CommonDataKinds.Event.CONTACT_ID + " = " + contactId; Cursor birthdayCursor = mContext.getContentResolver().query( ContactsContract.Data.CONTENT_URI, projection, selection, null, null); String birthday = null; if (birthdayCursor.moveToFirst()) { birthday = birthdayCursor.getString(birthdayCursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.DATA)); } birthdayCursor.close(); //得到联系人头像Bitamp Bitmap contactPhoto = null; //得到联系人头像ID int photoid = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_ID)); //photoid 大于0 表示联系人有头像 如果没有给此人设置头像则给他一个默认的 if (photoid > 0) { Uri uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, (long) contactId); InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(mContext.getContentResolver(), uri); contactPhoto = BitmapFactory.decodeStream(input); try { input.close(); } catch (IOException e) { e.printStackTrace(); } } else { contactPhoto = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.h001); } // 获取该联系人邮箱 //我这里只需要获取一个,没有遍历全部出来 Cursor cursorEmail = mContext.getContentResolver().query( ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null); String email = null; if (cursorEmail.moveToFirst()) { email = cursorEmail.getString( cursorEmail.getColumnIndex( ContactsContract.CommonDataKinds.Email.DATA)); } cursorEmail.close(); // 获取该联系人地址 //我这里只需要获取一个,没有遍历全部出来 Cursor cursorAddress = mContext.getContentResolver().query( ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, null, ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = " + contactId, null, null); String address = null; if (cursorAddress.moveToFirst()) { //获取CITY address = cursorAddress.getString(cursorAddress.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY)); } cursorAddress.close(); contact.setId(contactId); contact.setName(contactName); contact.setContactPhoto(contactPhoto); contact.setEmail(email); contact.setAddress(address); contact.setBirthday(birthday); // 查看该联系人有多少个电话号码。如果没有这返回值为0 int has_phone_number = cursor.getInt(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); if (has_phone_number > 0) { // 获得联系人的电话号码 Cursor cursorPhone = mContext.getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null); List<Phone> phones = new ArrayList<Phone>(); if (cursorPhone.moveToFirst()) { do { Phone phone = new Phone(); int phoneId = cursorPhone.getInt( cursorPhone.getColumnIndex( ContactsContract.CommonDataKinds.Phone._ID)); String phoneNumber = cursorPhone.getString( cursorPhone.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER)); phone.setContact(contact); phone.setId(phoneId); phone.setPhoneNumber(phoneNumber); //保存phone list集合 phones.add(phone); } while (cursorPhone.moveToNext()); } cursorPhone.close(); //如果有电话号码就把phones添加到contact上 contact.setPhones(phones); } contacts.add(contact); } while (cursor.moveToNext()); } cursor.close(); return contacts; } }
//另外2个bean类
public class Contact implements Serializable { private Integer id;//编号 private String name;//名字 private String birthday;//生日 private String email;//邮箱地址 private String address;//通讯地址 private Bitmap contactPhoto;//联系人照片 private String phoneNumber;//联系电话 private String sortLetters; //显示数据拼音的首字母 private String contactGroup;//联系组名称 private String objectId; private String createdAt; private ContactGroup mContactGroup; private MyUser myUser; private List<Phone> phones; //getset }
package org.mo.pmas.entity; import android.os.Parcel; import android.os.Parcelable; import java.io.Serializable; /** * Created by moziqi on 2015/1/9 0009. */ public class Phone implements Serializable { private Integer id; private String objectId; private String phoneNumber; private Contact contact; private MyUser myUser; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getObjectId() { return objectId; } public void setObjectId(String objectId) { this.objectId = objectId; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public Contact getContact() { return contact; } public void setContact(Contact contact) { this.contact = contact; } public MyUser getMyUser() { return myUser; } public void setMyUser(MyUser myUser) { this.myUser = myUser; } @Override public String toString() { return "Phone{" + "phoneNumber=‘" + phoneNumber + ‘\‘‘ + ", objectId=‘" + objectId + ‘\‘‘ + ", id=" + id + ‘}‘; } }
参考内容http://www.verydemo.com/demo_c131_i81641.html
http://www.eoeandroid.com/thread-24595-1-1.html
(android)解剖contentProvider获取通讯录列表-002
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。