• 读取手机本地全部联系人


        /**
         * 读取 联系人
         * @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;
        }
  • 相关阅读:
    POJ-1321-棋盘问题
    HDU-2553-N皇后问题
    POJ-1502-MPI Maelstrom
    POJ-3268-Silver Cow Party
    POJ-1125-Stockbroker Grapevine
    SPFA算法模板
    邻接表
    关于apache做301的问题
    在eclipse中使用正则表达式进行搜素
    sprintf数据库查询的作用
  • 原文地址:https://www.cnblogs.com/cbooy/p/4740087.html
Copyright © 2020-2023  润新知