安卓应用-如何获取联系人的生日[英] Android Applicaiton - How to get birthday of a contact

本文是小编为大家收集整理的关于安卓应用-如何获取联系人的生日的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在研究一个android应用程序,我需要匹配每次联系的生日,反对当前日期,如果是正面,请处理某些业务逻辑,需要完整的联系方式.

我已经找到了如何单独阅读联系人或联系人的生日,但我很困惑如何结合两者.有人可以提供一些方向.

感谢

推荐答案

在一些看网上后找到了答案.必须完成的方式是:

  • 获取联系人列表
  • 对于每个联系人,获取Contacter
  • 使用Chattern
  • 获得生日

以下是代码片段:

ContentResolver cr = getContentResolver(); //getContnetResolver()
String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME };

Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, projection, null, null,
            ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");

while (cur.moveToNext()) {

   Map<String, String> contactInfoMap = new HashMap<String, String>();
   String contactId = cur.getString(cur.getColumnIndex(ContactsContract.Data._ID));
   String displayName =  cur.getString(cur.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));     

    String columns[] = {
         ContactsContract.CommonDataKinds.Event.START_DATE,
         ContactsContract.CommonDataKinds.Event.TYPE,
         ContactsContract.CommonDataKinds.Event.MIMETYPE,
    };

    String where = Event.TYPE + "=" + Event.TYPE_BIRTHDAY + 
                    " and " + Event.MIMETYPE + " = '" + Event.CONTENT_ITEM_TYPE + "' and "                  + ContactsContract.Data.CONTACT_ID + " = " + contactId;

    String[] selectionArgs = null;
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME;

    Cursor birthdayCur = cr.query(ContactsContract.Data.CONTENT_URI, columns, where, selectionArgs, sortOrder); 
    if (birthdayCur.getCount() > 0) {
        while (birthdayCur.moveToNext()) {
             String birthday = birthdayCur.getString(birthdayCur.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
        }
    }
    birthdayCur.close();

   }    

    cur.close();

本文地址:https://www.itbaoku.cn/post/102635.html

问题描述

I am working on an android application for which I need to match the birthday of each contact against current date and if positive, process some business logic, which needs the complete contact details.

I have found ways to read birthdays of contacts or the contacts themselves separately, but am confused as to how to combine both. Can somebody please provide some direction.

Thanks

推荐答案

Found the answer after some looking out on the web. The way this has to be done is :

  • Get list of contacts
  • For each contact, get contactId
  • Get birthday using the contactid

Following is the code snippet :

ContentResolver cr = getContentResolver(); //getContnetResolver()
String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME };

Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, projection, null, null,
            ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");

while (cur.moveToNext()) {

   Map<String, String> contactInfoMap = new HashMap<String, String>();
   String contactId = cur.getString(cur.getColumnIndex(ContactsContract.Data._ID));
   String displayName =  cur.getString(cur.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));     

    String columns[] = {
         ContactsContract.CommonDataKinds.Event.START_DATE,
         ContactsContract.CommonDataKinds.Event.TYPE,
         ContactsContract.CommonDataKinds.Event.MIMETYPE,
    };

    String where = Event.TYPE + "=" + Event.TYPE_BIRTHDAY + 
                    " and " + Event.MIMETYPE + " = '" + Event.CONTENT_ITEM_TYPE + "' and "                  + ContactsContract.Data.CONTACT_ID + " = " + contactId;

    String[] selectionArgs = null;
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME;

    Cursor birthdayCur = cr.query(ContactsContract.Data.CONTENT_URI, columns, where, selectionArgs, sortOrder); 
    if (birthdayCur.getCount() > 0) {
        while (birthdayCur.moveToNext()) {
             String birthday = birthdayCur.getString(birthdayCur.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
        }
    }
    birthdayCur.close();

   }    

    cur.close();