码迷,mamicode.com
首页 > 其他好文 > 详细

读取联系人内容

时间:2015-11-04 13:16:21      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:

    private void readContacts() {
        final String name = "vnd.android.cursor.item/name";
        final String phone_v2 = "vnd.android.cursor.item/phone_v2";
        ContentResolver resolver = getContentResolver();
        Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
        Cursor cursor = resolver.query(uri, null, null, null, null);
        Uri dataUri = Uri.parse("content://com.android.contacts/data");
        while (cursor.moveToNext()) {
            String id = cursor.getString(cursor.getColumnIndex("contact_id"));
            if (id != null) {
                Cursor dataCursor = resolver.query(dataUri, null,
                        "raw_contact_id=?", new String[]{id}, null);
                Contacts contacts = new Contacts();
                contacts.setId(id);
                while (dataCursor.moveToNext()) {
                    String data1 = dataCursor.getString(dataCursor
                            .getColumnIndex("data1"));
                    String mimetype = dataCursor.getString(dataCursor
                            .getColumnIndex("mimetype"));
                    switch (mimetype) {
                        case name:
                            contacts.setName(data1);
                            break;
                        case phone_v2:
                            contacts.setPhone(data1);
                            break;
                    }
                }
                System.out.println(contacts);
            }
        }
    }

技术分享

<uses-permission android:name="android.permission.READ_CONTACTS" />

 

读取联系人内容

标签:

原文地址:http://www.cnblogs.com/linson0116/p/4935589.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!