/**
 * 获取联系人
 * @return
 */
public static List<ContactInfo> getContactInfos(Context context) {
    ContentResolver resolver = context.getContentResolver();
    Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
    Uri dataUri = Uri.parse("content://com.android.contacts/data");
    List<ContactInfo> contactInfos = new ArrayList<ContactInfo>();
    Cursor cursor = resolver.query(uri, new String[] { "contact_id" },
            null, null, null);
    while (cursor.moveToNext()) {
        String id = cursor.getString(0);
        if (id != null) {
            ContactInfo info = new ContactInfo();
            Cursor dataCursor = resolver.query(dataUri, new String[] {
                    "mimetype", "data1" }, "raw_contact_id=?",
                    new String[] { id }, null);
            while (dataCursor.moveToNext()) {
                if("vnd.android.cursor.item/name".equals( dataCursor.getString(0))){
                    info.setName(dataCursor.getString(1));
                }else if("vnd.android.cursor.item/phone_v2".equals( dataCursor.getString(0))){
                    info.setPhone(dataCursor.getString(1));
                }
            }
            contactInfos.add(info);
            dataCursor.close();
        }
    }
    cursor.close();
    return contactInfos;
}

 

要使用到这三个表,但是谷歌内部在查询这个data表的时候内部使用的并不是查询data表而是查询了data表的视图,这个视图中直接将mime_Type类型给关联好了,所以这里直接查询的就是mimetype这个类型就可以了,还有就是如果自己的手机中如果删除了一个联系人在查询的时候就会报错,这里因为是在手机中删除一个联系人的时候并不是清空数据库中的数据,而是将这个raw_contacks中的id置为null,所以会报错,这里就要进行判断一下查出来的id是否为null。

 

posted on 2015-05-06 10:02  道无涯  阅读(264)  评论(0编辑  收藏  举报