码迷,mamicode.com
首页 > 移动开发 > 详细

读取手机本地全部联系人

时间:2015-08-18 19:05:01      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

    /**
     * 读取 联系人
     * @param context
     * @return
     */
    public static List<Map<String, String>> getLocalContacts(Context context) {
        
        List<Map<String, String>> datas = new ArrayList<Map<String,String>>();
        
        ContentResolver cr = context.getContentResolver();
        
        Uri uriRawContacts = Uri.parse("content://com.android.contacts/raw_contacts");
        
        Uri uriData = Uri.parse("content://com.android.contacts/data");
        
        Cursor rawContactsCursor = cr.query(uriRawContacts, 
                new String[] { "contact_id"}, 
                null, 
                null, 
                null);
        
        String data1 = null;
        
        String mimetype = null;
        
        while(rawContactsCursor.moveToNext()){
            String contactId = rawContactsCursor.getString(rawContactsCursor.getColumnIndex("contact_id"));
            
            if(contactId != null){
                
                Map<String, String> data = new HashMap<String,String>();
                
                Cursor dataCursor = cr.query(uriData, 
                        new String[]{"data1","mimetype"}, 
                        "contact_id=?", 
                        new String[]{contactId}, 
                        null);
                
                while(dataCursor.moveToNext()){
                    
                    data1 = dataCursor.getString(dataCursor.getColumnIndex("data1"));
                    mimetype = dataCursor.getString(dataCursor.getColumnIndex("mimetype"));
                    
                    if("vnd.android.cursor.item/name".equals(mimetype)){
                        data.put("name", data1);
                        //Log.i(StaticDatas.CONTACTSHELPERUTIL_LOG_TAG, "name is " + data1);
                    }else if("vnd.android.cursor.item/phone_v2".equals(mimetype)){
                        data.put("phone", data1);
                        //Log.i(StaticDatas.CONTACTSHELPERUTIL_LOG_TAG, "phone is " + data1);
                    }
                }
                
                datas.add(data);
                
                dataCursor.close();
            }
        }
        
        rawContactsCursor.close();
        
        return datas;
    }

 

读取手机本地全部联系人

标签:

原文地址:http://www.cnblogs.com/cbooy/p/4740087.html

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