ContactsContract.CommonDataKinds.Event.START_DATE 包括联系电话号码

Posted

技术标签:

【中文标题】ContactsContract.CommonDataKinds.Event.START_DATE 包括联系电话号码【英文标题】:ContactsContract.CommonDataKinds.Event.START_DATE includes contact phone number 【发布时间】:2021-07-01 09:36:14 【问题描述】:

我使用以下代码检索联系人的所有事件:

Uri uri = ContactsContract.Data.CONTENT_URI;
ContentResolver cr = context.getContentResolver();
String[] projection = new String[]
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Event.CONTACT_ID,
            ContactsContract.CommonDataKinds.Event.START_DATE,
            ContactsContract.CommonDataKinds.Event.TYPE,
            ContactsContract.CommonDataKinds.Event.DATA3;

String where = ContactsContract.Data.MIMETYPE + "= ? AND "
            + ContactsContract.CommonDataKinds.Event.TYPE + "="
            + ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY + " OR "
            + ContactsContract.CommonDataKinds.Event.TYPE + "="
            + ContactsContract.CommonDataKinds.Event.TYPE_OTHER + " OR "
            + ContactsContract.CommonDataKinds.Event.TYPE + "="
            + ContactsContract.CommonDataKinds.Event.TYPE_CUSTOM;
String[] selectionArgs = new String[] 
ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE;

问题是联系人的电话号码被列为 Event.START_DATE 和 TYPE_OTHER。

有什么办法可以排除它们,只获取真实事件?

【问题讨论】:

【参考方案1】:

我认为您应该在 where 子句中添加澄清括号,因为它现在可能包含具有非事件 mimetype 的行(例如 CommonDataKinds.Phone),只要它们具有 TYPE = CUSTOM。

试试这个:

String where = Data.MIMETYPE + "= ? AND "
            + "("
            + Event.TYPE + "="
            + Event.TYPE_ANNIVERSARY + " OR "
            + Event.TYPE + "="
            + Event.TYPE_OTHER + " OR "
            + Event.TYPE + "="
            + Event.TYPE_CUSTOM
            + ")";
String[] selectionArgs = new String[] 
Event.CONTENT_ITEM_TYPE;

【讨论】:

非常感谢!现在完美运行!因为这些括号,我失去了一天的生命。【参考方案2】:
private ArrayList<String> getEventday(String contactId) 
    String eventday = null;
    ArrayList<String> eventlist = new ArrayList<String>();
    ContentResolver cr = getContentResolver();
    Uri uri = ContactsContract.Data.CONTENT_URI;
    String[] projection = new String[]
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Event.CONTACT_ID,
            ContactsContract.CommonDataKinds.Event.START_DATE,
            ContactsContract.CommonDataKinds.Event.TYPE,
            ContactsContract.CommonDataKinds.Event.DATA3;
    String orgWhere = ContactsContract.Data.CONTACT_ID + " = ? AND "
            + ContactsContract.Data.MIMETYPE + " = ? AND"
            + "("
            + ContactsContract.CommonDataKinds.Event.TYPE + "="
            + ContactsContract.CommonDataKinds.Event.TYPE_ANNIVERSARY + " OR "
            + ContactsContract.CommonDataKinds.Event.TYPE + "="
            + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY + " OR "
            + ContactsContract.CommonDataKinds.Event.TYPE + "="
            + ContactsContract.CommonDataKinds.Event.TYPE_OTHER
            + ")";


    String[] selectionArgs = new String[]
            contactId,ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE;

    String sortOrder = null;
    Cursor pCur = cr.query(uri, projection, orgWhere, selectionArgs, sortOrder);
    while (pCur.moveToNext()) 

        String eventytype = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Event.TYPE));
        eventday = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));



        ContactAddPhoneBook contactAddPhoneBook=new ContactAddPhoneBook();
        String typeEvent=contactAddPhoneBook.getContentTypeCustomerlistEventtype(Integer.parseInt(eventytype));

        eventlist.add("(" + typeEvent + ")" + ":" +eventday);
    
    pCur.close();
    return eventlist;

【讨论】:

希望对您有所帮助。根据联系人获取活动日期

以上是关于ContactsContract.CommonDataKinds.Event.START_DATE 包括联系电话号码的主要内容,如果未能解决你的问题,请参考以下文章

如何只获取联系人的手机号码