如何获得联系,以便他们即将到来的生日吗?如何获得、生日

由网友(柠蓝浅夏)分享简介:我有code阅读详细联系方式和阅读的生日。但是,我怎么才能自己即将到来的生日的联系人列表?有关确定标识单个联系人,我得到的信息和生日是这样的:光标C = NULL;尝试 {开放的我们的uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_U...

我有code阅读详细联系方式和阅读的生日。但是,我怎么才能自己即将到来的生日的联系人列表?

有关确定标识单个联系人,我得到的信息和生日是这样的:

 光标C = NULL;
  尝试 {
   开放的我们的uri = ContentUris.withAppendedId(
     ContactsContract.Contacts.CONTENT_URI,身份证);
   C = ctx.getContentResolver()查询(URI,NULL,NULL,NULL,NULL);
   如果(C!= NULL){
    如果(c.moveToFirst()){
     DatabaseUtils.cursorRowToContentValues​​(C,数据);
    }

   }
   c.close();

   //读取生日
   C = ctx.getContentResolver()
     .query(
       Data.CONTENT_URI,
       新的String [] {} Event.DATA,
       Data.CONTACT_ID +=+ ID +和
         + Data.MIMETYPE +=
         + Event.CONTENT_ITEM_TYPE +'和
         + Event.type访问+=+ Event.TYPE_BIRTHDAY,
       空,Data.DISPLAY_NAME);
   如果(C!= NULL){
    尝试 {
     如果(c.moveToFirst()){
      this.setBirthday(c.getString(0));
     }
    } 最后 {
     c.close();
    }
   }

   返回super.load(ID);
  }赶上(例外五){
   Log.v(TAG(),e.​​getMessage(),E);
   e.printStackTrace();
   返回false;
  } 最后 {
   如果(C!= NULL)
    c.close();
  }
 

和code读取所有的联系人是:

 公开光标的GetList(){
        //获取基URI在联系人内容People表
        //提供商。
        乌里接触= ContactsContract.Contacts.CONTENT_URI;
        //使查询。
        ContentResolver的CR = ctx.getContentResolver();
        //形成一个阵列指定列返回的。
        的String []投影=新的String [] {ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME};

        光标managedCursor = cr.query(联系人,投影,NULL,NULL,
                ContactsContract.Contacts.DISPLAY_NAME
                        +分页中局部ASC);
        返回managedCursor;
    }
 
520生日会,我们不见不散

解决方案

这个我没有倒过来的 - 它存储的生日直接选择到数据表

 私有静态最终诠释UPCOMING_COUNT = 10;


公共静态列表< BContact> upcomingBirthday(上下文CTX){
    。今天的String =新的SimpleDateFormat(MM-DD)格式(新的日期());
    名单< BContact> firstPart = upcomingBirthday(CTX,今天,12-31,UPCOMING_COUNT);
    如果(firstPart.size()&其中; UPCOMING_COUNT){
        firstPart.addAll(upcomingBirthday(CTX,01-01,今天,UPCOMING_COUNT  -  firstPart.size()));
    }
    返回firstPart;
}


公共静态列表< BContact> upcomingBirthday(上下文CTX,串数fromdate,字符串TODATE,诠释行){

  乌里dataUri = ContactsContract.Data.CONTENT_URI;

  的String []投影=新的String [] {ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Event.CONTACT_ID,
            ContactsContract.CommonDataKinds.Event.START_DATE
            };


  光标C = ctx.getContentResolver()查询(
       dataUri,
       投影,
       ContactsContract.Data.MIMETYPE +=?和+
       ContactsContract.CommonDataKinds.Event.TYPE +=+ ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY +
       与SUBSTR(+ ContactsContract.CommonDataKinds.Event.START_DATE +,6)> =? +
       与SUBSTR(+ ContactsContract.CommonDataKinds.Event.START_DATE +,6)&其中; =? ,
       新的String [] {ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE,数fromdate,TODATE},
       SUBSTR(+ ContactsContract.CommonDataKinds.Event.START_DATE +,6));
  名单< BContact>结果=新的ArrayList< BContact>();
  INT I = 0;
  而(c.moveToNext()&安培;&安培; I&其中;行){
      result.add(新BContact(c.getString(0),c.getString(1),c.getString(2)));
      我++;
  }
  c.close();
  返回结果;
 

}

在SELECT被调用两次 - 一次调用从今天到12/31和第二个电话从01/01到今天。我限制返回行至10,但这不是necesarry。

编辑:我发现,这不会对所有Android手机正常工作的生日日期都存储在许多不同的格式。所以,你必须加载所有的生日(无序),解析它和排序它在内存中。 #Fail

I have code to read contact details and to read birthdays. But how do I get a list of contacts in order of their upcoming birthday?

For a single contact identified by id, I get details and birthday like this:

Cursor c = null;
  try {
   Uri uri = ContentUris.withAppendedId(
     ContactsContract.Contacts.CONTENT_URI, id);
   c = ctx.getContentResolver().query(uri, null, null, null, null);
   if (c != null) {
    if (c.moveToFirst()) {
     DatabaseUtils.cursorRowToContentValues(c, data);
    }

   }
   c.close();

   // read birthday
   c = ctx.getContentResolver()
     .query(
       Data.CONTENT_URI,
       new String[] { Event.DATA },
       Data.CONTACT_ID + "=" + id + " AND "
         + Data.MIMETYPE + "= '"
         + Event.CONTENT_ITEM_TYPE + "' AND "
         + Event.TYPE + "=" + Event.TYPE_BIRTHDAY,
       null, Data.DISPLAY_NAME);
   if (c != null) {
    try {
     if (c.moveToFirst()) {
      this.setBirthday(c.getString(0));
     }
    } finally {
     c.close();
    }
   }

   return super.load(id);
  } catch (Exception e) {
   Log.v(TAG(), e.getMessage(), e);
   e.printStackTrace();
   return false;
  } finally {
   if (c != null)
    c.close();
  }

and the code to read all contacts is:

public Cursor getList() {
        // Get the base URI for the People table in the Contacts content
        // provider.
        Uri contacts = ContactsContract.Contacts.CONTENT_URI;
        // Make the query.
        ContentResolver cr = ctx.getContentResolver();
        // Form an array specifying which columns to return.
        String[] projection = new String[] { ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME };

        Cursor managedCursor = cr.query(contacts, projection, null, null,
                ContactsContract.Contacts.DISPLAY_NAME
                        + " COLLATE LOCALIZED ASC");
        return managedCursor;
    }

解决方案

I did this the other way round - a selection directly to Data table which stores birthday.

private static final int UPCOMING_COUNT = 10;


public static List<BContact> upcomingBirthday(Context ctx) {
    String today = new SimpleDateFormat("MM-dd").format(new Date());        
    List<BContact> firstPart = upcomingBirthday(ctx, today, "12-31", UPCOMING_COUNT);
    if (firstPart.size() < UPCOMING_COUNT) {
        firstPart.addAll(upcomingBirthday(ctx, "01-01", today, UPCOMING_COUNT - firstPart.size()));
    }
    return firstPart;
}


public static List<BContact> upcomingBirthday(Context ctx, String fromDate, String toDate, int rows) {

  Uri dataUri = ContactsContract.Data.CONTENT_URI;

  String[] projection = new String[] { ContactsContract.Contacts.DISPLAY_NAME,                         
            ContactsContract.CommonDataKinds.Event.CONTACT_ID,
            ContactsContract.CommonDataKinds.Event.START_DATE
            };


  Cursor c = ctx.getContentResolver().query(
       dataUri,
       projection, 
       ContactsContract.Data.MIMETYPE + "= ? AND " + 
       ContactsContract.CommonDataKinds.Event.TYPE + "=" + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY + 
       " AND substr(" + ContactsContract.CommonDataKinds.Event.START_DATE + ",6) >= ?" + 
       " AND substr(" + ContactsContract.CommonDataKinds.Event.START_DATE + ",6) <= ?" ,
       new String[] {ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE, fromDate, toDate}, 
       "substr("+ ContactsContract.CommonDataKinds.Event.START_DATE +",6)");
  List<BContact> result = new ArrayList<BContact>();
  int i=0;
  while (c.moveToNext() && i<rows) {
      result.add(new BContact(c.getString(0), c.getString(1), c.getString(2)));
      i++;
  }
  c.close();
  return result;

}

The SELECT is called twice - one call from today to 12/31 and second call from 01/01 to today. I limit returned rows to 10 but this is not necesarry.

EDIT: I found that this won't work on all Android phones as birthday dates are stored in many different format. So you have to load all birthdays (unordered), parse it and sort it in memory. #Fail

阅读全文

相关推荐

最新文章