查询联系人

查询联系人 

  • 微信 QQ 陌陌…. 
  • 联系人相关的三张表 
    • data表 data1列 存的是联系人所有的信息(包括 电话 邮箱 姓名 地址) 
    • raw_contacts表 contact_id列 表示应用一共有几条联系人 
    • mimetype 表 id字段 用来区分联系人信息 
  • 查询联系人的步骤 
    • 先查询raw_contacts 的contact_id字段 就知道一共有几条联系人 
    • 在查询data表 先查询data1字段和mimetype_id字段 
  • Invalid column mimetype_id() :报mimetype_id列无效 1:列名写错了 2.表中没有这列.
    public static List<Contact> getContactList(Context context){
        ArrayList<Contact> lists = new ArrayList<>();
        Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
        Uri dataUri = Uri.parse("content://com.android.contacts/data");
        Cursor cursor = context.getContentResolver().query(uri, new String[]{"contact_id"}, null, null, null);
        while(cursor.moveToNext()){
            Contact contact = new Contact();
            String value = cursor.getString(0);
//            Log.e(TAG, "getContactList: "+value );
            Cursor datacursor = context.getContentResolver().query(dataUri, new String[]{"data1", "mimetype"}, "raw_contact_id = ?", new String[]{value}, null);
            while(datacursor.moveToNext()){
                String data1 = datacursor.getString(0);
                Log.e(TAG, "getContactList: +++++++++++++"+datacursor );
                String mimetype = datacursor.getString(1);
                Log.e(TAG, "getContactList: "+mimetype );
                if (mimetype.equals("vnd.android.cursor.item/phone_v2")){
                    contact.setPhone(data1);
                }else if(mimetype.equals("vnd.android.cursor.item/name")){
                    contact.setName(data1);
                }else if(mimetype.equals("vnd.android.cursor.item/email_v2")){
                    contact.setEmali(data1);
                }
            }
            lists.add(contact);
        }
        return lists;
    }

猜你喜欢

转载自www.cnblogs.com/nangongyibin/p/10249526.html