ContactsContract.CommonDataKinds.StructuredName 的问题

发布于 2024-10-06 13:46:04 字数 8203 浏览 2 评论 0原文

在查询联系人数据时,我的大部分工作都完成了。我正在与 StructuredName 和 Organization 作斗争。他们不为我工作。

如果您不介意,请看一下代码。它是手工制作的,并且删除了许多错误检查,以使其尽可能小,以便在此处发布。带有“不起作用”的评论是对我不起作用的部分。

两个前导 if() 是 onActivityResult 的一部分,我在联系人选择结束时接收数据。通话结束:

intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, Main.DIALOG_PICKCONTACT);

提前非常感谢 和家伟

if (requestCode == Main.DIALOG_PICKCONTACT) {
 if (resultCode == RESULT_OK) {
  int i;
  String s;
  Cursor cursorContacts = managedQuery(intent.getData(),
      null,
      null,
      null,
      null);
  if (cursorContacts != null) {
   if (cursorContacts.moveToNext()) {
    ContentResolver contentResolver = getContentResolver();
    String contactId = cursorContacts.getString(cursorContacts.getColumnIndex(ContactsContract.Contacts._ID));
    i = cursorContacts.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
    // cursorContacts.getString(i); *** is DISPLAY_NAME
    Cursor cursorEmail = contentResolver.query(
        ContactsContract.CommonDataKinds.Email.CONTENT_URI,
        null,
        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
        new String[] { contactId },
        null);
    if (cursorEmail != null) {
     while (cursorEmail.moveToNext()) {
      int type = cursorEmail.getInt(cursorEmail.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
      s = cursorEmail.getString(cursorEmail.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
      switch (type) {
       case ContactsContract.CommonDataKinds.Email.TYPE_HOME:
        // s *** is private Email
        break;
       case ContactsContract.CommonDataKinds.Email.TYPE_WORK:
        // s *** is business Email
        break;
      }
     }
     cursorEmail.close();
    }
    Cursor cursorOrganization = contentResolver.query(
        ContactsContract.Data.CONTENT_URI,
        null,
        ContactsContract.CommonDataKinds.Organization.CONTACT_ID + " = ?",
        new String[] { contactId },
        null);
    if (cursorOrganization != null) {
     if (cursorOrganization.moveToNext()) {
      s = cursorOrganization.getString(cursorOrganization.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));
      // s *** Company name not working
      s = cursorOrganization.getString(cursorOrganization.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
      // s *** Function within company not working
     }
     cursorOrganization.close();
    }
    s = cursorContacts.getString(cursorContacts.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
    if (Integer.parseInt(s) > 0) {
     Cursor cursorPhone = contentResolver.query(
         ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
         null,
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
         new String[] { contactId },
         null);
     if (cursorPhone != null) {
      while (cursorPhone.moveToNext()) {
       int type = cursorPhone.getInt(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
       s = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
       switch (type) {
        case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME:
         // s *** is private fax
         break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK:
         // s *** is business fax
         break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
         // s *** is private phone
         break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
         // s *** is mobile phone
         break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
         // s *** is business phone
         break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE:
         // s *** is business mobile
         break;
       }
      }
      cursorPhone.close();
     }
    }
    Cursor cursorStructuredName = contentResolver.query(
        ContactsContract.Data.CONTENT_URI,
        null,
        ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = ?",
        new String[] { contactId },
        null);
    if (cursorStructuredName != null) {
     if (cursorStructuredName.moveToNext()) {
      i = cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
      // cursorStructuredName.getString(i); *** given name not working
      i = cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME);
      // cursorStructuredName.getString(i); *** middle name not working
      i = cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.PREFIX);
      // cursorStructuredName.getString(i); *** prefix not working
      i = cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.SUFFIX);
      // cursorStructuredName.getString(i); *** suffix not working
     }
     cursorStructuredName.close();
    }
    Cursor cursorStructuredPostal = contentResolver.query(
        ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
        null,
        ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = ?",
        new String[] { contactId },
        null);
    if (cursorStructuredPostal != null) {
     while (cursorStructuredPostal.moveToNext()) {
      int type = cursorStructuredPostal.getInt(cursorStructuredPostal.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
      s = cursorStructuredPostal.getString(cursorStructuredPostal.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
      switch (type) {
       case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME:
        //s *** is private country
        break;
       case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK:
        // s *** is business country
        break;
      }
      s = cursorStructuredPostal.getString(cursorStructuredPostal.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
      switch (type) {
       case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME:
        // s *** is private city
        break;
       case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK:
        // s *** is business city
        break;
      }
      s = cursorStructuredPostal.getString(cursorStructuredPostal.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
      switch (type) {
       case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME:
        // s *** is private postcode
        break;
       case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK:
        // s *** is business postcode
        break;
      }
      s = cursorStructuredPostal.getString(cursorStructuredPostal.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
      switch (type) {
       case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME:
        // s *** is private street
        break;
       case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK:
        // s *** is business street
        break;
      }
     }
     cursorStructuredPostal.close();
    }
    Cursor cursorWebsite = contentResolver.query(
        ContactsContract.Data.CONTENT_URI,
        null,
        ContactsContract.CommonDataKinds.Website.CONTACT_ID + " = ?",
        new String[] { contactId },
        null);
    if (cursorWebsite != null) {
     while (cursorWebsite.moveToNext()) {
      i = cursorWebsite.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL);
      int type = cursorWebsite.getColumnIndex(ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE);
      s = cursorWebsite.getString(i);
      switch (type) {
       case ContactsContract.CommonDataKinds.Website.TYPE_HOME:
        // s *** private url not working
        break;
       case ContactsContract.CommonDataKinds.Website.TYPE_WORK:
        // s *** business url not working
        break;
      }
     }
     cursorWebsite.close();
    }
   }
   cursorContacts.close();
  }
 }
}

I got most of the things working when querying contact data. I'm fighting with StructuredName and Organization. They do not work for me.

If you don't mind please have a look at the code. It's handcrafted and lot of error checking is removed to make it as small as possible to be posted here. Comments with "not working" are the parts that don't work for me.

The two leading if() are part of the onActivityResult where I receive the data at the end of the contact selection. Calling is done with:

intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, Main.DIALOG_PICKCONTACT);

Many thanks in advance
HJW

if (requestCode == Main.DIALOG_PICKCONTACT) {
 if (resultCode == RESULT_OK) {
  int i;
  String s;
  Cursor cursorContacts = managedQuery(intent.getData(),
      null,
      null,
      null,
      null);
  if (cursorContacts != null) {
   if (cursorContacts.moveToNext()) {
    ContentResolver contentResolver = getContentResolver();
    String contactId = cursorContacts.getString(cursorContacts.getColumnIndex(ContactsContract.Contacts._ID));
    i = cursorContacts.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
    // cursorContacts.getString(i); *** is DISPLAY_NAME
    Cursor cursorEmail = contentResolver.query(
        ContactsContract.CommonDataKinds.Email.CONTENT_URI,
        null,
        ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
        new String[] { contactId },
        null);
    if (cursorEmail != null) {
     while (cursorEmail.moveToNext()) {
      int type = cursorEmail.getInt(cursorEmail.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
      s = cursorEmail.getString(cursorEmail.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
      switch (type) {
       case ContactsContract.CommonDataKinds.Email.TYPE_HOME:
        // s *** is private Email
        break;
       case ContactsContract.CommonDataKinds.Email.TYPE_WORK:
        // s *** is business Email
        break;
      }
     }
     cursorEmail.close();
    }
    Cursor cursorOrganization = contentResolver.query(
        ContactsContract.Data.CONTENT_URI,
        null,
        ContactsContract.CommonDataKinds.Organization.CONTACT_ID + " = ?",
        new String[] { contactId },
        null);
    if (cursorOrganization != null) {
     if (cursorOrganization.moveToNext()) {
      s = cursorOrganization.getString(cursorOrganization.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DATA));
      // s *** Company name not working
      s = cursorOrganization.getString(cursorOrganization.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE));
      // s *** Function within company not working
     }
     cursorOrganization.close();
    }
    s = cursorContacts.getString(cursorContacts.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
    if (Integer.parseInt(s) > 0) {
     Cursor cursorPhone = contentResolver.query(
         ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
         null,
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
         new String[] { contactId },
         null);
     if (cursorPhone != null) {
      while (cursorPhone.moveToNext()) {
       int type = cursorPhone.getInt(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
       s = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
       switch (type) {
        case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME:
         // s *** is private fax
         break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK:
         // s *** is business fax
         break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
         // s *** is private phone
         break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
         // s *** is mobile phone
         break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
         // s *** is business phone
         break;
        case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE:
         // s *** is business mobile
         break;
       }
      }
      cursorPhone.close();
     }
    }
    Cursor cursorStructuredName = contentResolver.query(
        ContactsContract.Data.CONTENT_URI,
        null,
        ContactsContract.CommonDataKinds.StructuredName.CONTACT_ID + " = ?",
        new String[] { contactId },
        null);
    if (cursorStructuredName != null) {
     if (cursorStructuredName.moveToNext()) {
      i = cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
      // cursorStructuredName.getString(i); *** given name not working
      i = cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME);
      // cursorStructuredName.getString(i); *** middle name not working
      i = cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.PREFIX);
      // cursorStructuredName.getString(i); *** prefix not working
      i = cursorStructuredName.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.SUFFIX);
      // cursorStructuredName.getString(i); *** suffix not working
     }
     cursorStructuredName.close();
    }
    Cursor cursorStructuredPostal = contentResolver.query(
        ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
        null,
        ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = ?",
        new String[] { contactId },
        null);
    if (cursorStructuredPostal != null) {
     while (cursorStructuredPostal.moveToNext()) {
      int type = cursorStructuredPostal.getInt(cursorStructuredPostal.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
      s = cursorStructuredPostal.getString(cursorStructuredPostal.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
      switch (type) {
       case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME:
        //s *** is private country
        break;
       case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK:
        // s *** is business country
        break;
      }
      s = cursorStructuredPostal.getString(cursorStructuredPostal.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
      switch (type) {
       case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME:
        // s *** is private city
        break;
       case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK:
        // s *** is business city
        break;
      }
      s = cursorStructuredPostal.getString(cursorStructuredPostal.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
      switch (type) {
       case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME:
        // s *** is private postcode
        break;
       case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK:
        // s *** is business postcode
        break;
      }
      s = cursorStructuredPostal.getString(cursorStructuredPostal.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
      switch (type) {
       case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_HOME:
        // s *** is private street
        break;
       case ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK:
        // s *** is business street
        break;
      }
     }
     cursorStructuredPostal.close();
    }
    Cursor cursorWebsite = contentResolver.query(
        ContactsContract.Data.CONTENT_URI,
        null,
        ContactsContract.CommonDataKinds.Website.CONTACT_ID + " = ?",
        new String[] { contactId },
        null);
    if (cursorWebsite != null) {
     while (cursorWebsite.moveToNext()) {
      i = cursorWebsite.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL);
      int type = cursorWebsite.getColumnIndex(ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE);
      s = cursorWebsite.getString(i);
      switch (type) {
       case ContactsContract.CommonDataKinds.Website.TYPE_HOME:
        // s *** private url not working
        break;
       case ContactsContract.CommonDataKinds.Website.TYPE_WORK:
        // s *** business url not working
        break;
      }
     }
     cursorWebsite.close();
    }
   }
   cursorContacts.close();
  }
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烏雲後面有陽光 2024-10-13 13:46:04

你的where条件是错误的。

使用它作为 StructuredName:

    // projection
    String[] projection = new String[] {ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME};


    String where = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
    String[] whereParameters = new String[]{this.contact_id, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};

    //Request
    Cursor contacts = this.resolver.query(ContactsContract.Data.CONTENT_URI, projection, where, whereParameters, null);

    //Iteration
    if (contacts.moveToFirst()) { 
        //code here
    } 
    contacts.close();

Your where condition is wrong.

Use this for StructuredName:

    // projection
    String[] projection = new String[] {ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME};


    String where = ContactsContract.Data.RAW_CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
    String[] whereParameters = new String[]{this.contact_id, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE};

    //Request
    Cursor contacts = this.resolver.query(ContactsContract.Data.CONTENT_URI, projection, where, whereParameters, null);

    //Iteration
    if (contacts.moveToFirst()) { 
        //code here
    } 
    contacts.close();
无可置疑 2024-10-13 13:46:04

无法找到完整的解决方案如何获取从联系人中挑选的人的名字、姓氏、中间名和生日。所以这是我的解决方案:

显示联系人列表:

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, GET_PHONE_ADDRESS_BOOK_CONST);

选择联系人:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == Activity.RESULT_OK) { 
        switch (requestCode) {

        case GET_PHONE_ADDRESS_BOOK_CONST:
            getContactFromAddressBook(data);
            break;


        default:
            break;
        }
    } 
    super.onActivityResult(requestCode, resultCode, data);
}

获取联系人 ID:

private void getContactFromAddressBook(Intent data) {

    if (data != null) {
        Uri uri = data.getData();
        if (uri != null) {
            Cursor c = null;
            try {               
                c = getActivity().getContentResolver().query(uri,
                        null, null, null, null);

                if (c != null && c.moveToFirst()) {
                    String id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
                    getFullName(id);
                    getBirthday(id);
                }
            } 
            catch(Exception e) {
                Utils.Loge(e.getMessage(), e);
            }
            finally {
                if (c != null) {
                    c.close();
                }
            }
        }
    }
}

获取第一个、最后一个和中间名

private HashMap<String, String> getFullName(String id)
{
    HashMap<String, String> ret = new HashMap<String, String>();

    String whereName = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
    String[] whereNameParams = new String[] { id, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
    Cursor c = null;
    try {
        c = getActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, null);
        if (c != null && c.moveToFirst()) {
            int indexGivenName = c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
            int indexFamilyName = c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME);
            int indexDisplayName = c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME);    

            ret.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, c.getString(indexGivenName));
            ret.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, c.getString(indexFamilyName));
            ret.put(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, c.getString(indexDisplayName));
        }
    } 
    catch(Exception e) {
        Utils.Loge(e.getMessage(), e);
    }
    finally {
        if (c != null) {
            c.close();
        }
    }
    return ret;
}

获取生日

private Date getBirthday(String id)
{
    Date date = null;

    String whereName = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.Event.TYPE + " = ?"; 
    String[] whereNameParams = new String[] { id, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE, String.valueOf(ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY) };
    Cursor c = null;
    try {
        c = getActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, null);
        if (c != null && c.moveToFirst()) {
            int indexBirthday = c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Event.START_DATE); 
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
            date = format.parse(c.getString(indexBirthday));
        }
    } 
    catch(Exception e) {
        Utils.Loge(e.getMessage(), e);
    }
    finally {
        if (c != null) {
            c.close();
        }
    }
    return date;
}

Couldn't find complete solution how to get first, last, middle name and birthday of a person picked from contacts. So here is my solution:

Show a contacts' list:

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, GET_PHONE_ADDRESS_BOOK_CONST);

Pick up a contact:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == Activity.RESULT_OK) { 
        switch (requestCode) {

        case GET_PHONE_ADDRESS_BOOK_CONST:
            getContactFromAddressBook(data);
            break;


        default:
            break;
        }
    } 
    super.onActivityResult(requestCode, resultCode, data);
}

Get contact's id:

private void getContactFromAddressBook(Intent data) {

    if (data != null) {
        Uri uri = data.getData();
        if (uri != null) {
            Cursor c = null;
            try {               
                c = getActivity().getContentResolver().query(uri,
                        null, null, null, null);

                if (c != null && c.moveToFirst()) {
                    String id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
                    getFullName(id);
                    getBirthday(id);
                }
            } 
            catch(Exception e) {
                Utils.Loge(e.getMessage(), e);
            }
            finally {
                if (c != null) {
                    c.close();
                }
            }
        }
    }
}

Get first, last and middle names

private HashMap<String, String> getFullName(String id)
{
    HashMap<String, String> ret = new HashMap<String, String>();

    String whereName = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?"; 
    String[] whereNameParams = new String[] { id, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE };
    Cursor c = null;
    try {
        c = getActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, null);
        if (c != null && c.moveToFirst()) {
            int indexGivenName = c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME);
            int indexFamilyName = c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME);
            int indexDisplayName = c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME);    

            ret.put(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME, c.getString(indexGivenName));
            ret.put(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME, c.getString(indexFamilyName));
            ret.put(ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME, c.getString(indexDisplayName));
        }
    } 
    catch(Exception e) {
        Utils.Loge(e.getMessage(), e);
    }
    finally {
        if (c != null) {
            c.close();
        }
    }
    return ret;
}

Get birthday

private Date getBirthday(String id)
{
    Date date = null;

    String whereName = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ? AND " + ContactsContract.CommonDataKinds.Event.TYPE + " = ?"; 
    String[] whereNameParams = new String[] { id, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE, String.valueOf(ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY) };
    Cursor c = null;
    try {
        c = getActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI, null, whereName, whereNameParams, null);
        if (c != null && c.moveToFirst()) {
            int indexBirthday = c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Event.START_DATE); 
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
            date = format.parse(c.getString(indexBirthday));
        }
    } 
    catch(Exception e) {
        Utils.Loge(e.getMessage(), e);
    }
    finally {
        if (c != null) {
            c.close();
        }
    }
    return date;
}
古镇旧梦 2024-10-13 13:46:04
private String getWebsite(String contactId)
{
    String[] cols = { ContactsContract.CommonDataKinds.Website.URL };
    String filter = ContactsContract.Data.CONTACT_ID+" = ? " +
                    " and "+ContactsContract.Data.MIMETYPE
                           +" = '"+ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE+"'";
    String[] params = { String.valueOf(contactId) };
    Cursor website = act.managedQuery(ContactsContract.Data.CONTENT_URI, cols, filter, params, null);

    while (website.moveToNext()) {
        String val = website.getString(website.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL));
        if (!StringUtils.isEmpty(val)) {
            return val;
        }
    }
    return null;
}

// abb2yb:现在的启蒙

private String getWebsite(String contactId)
{
    String[] cols = { ContactsContract.CommonDataKinds.Website.URL };
    String filter = ContactsContract.Data.CONTACT_ID+" = ? " +
                    " and "+ContactsContract.Data.MIMETYPE
                           +" = '"+ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE+"'";
    String[] params = { String.valueOf(contactId) };
    Cursor website = act.managedQuery(ContactsContract.Data.CONTENT_URI, cols, filter, params, null);

    while (website.moveToNext()) {
        String val = website.getString(website.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL));
        if (!StringUtils.isEmpty(val)) {
            return val;
        }
    }
    return null;
}

// abb2yb: enlightenmentnow

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文