2011-12-20 7 views

답변

22

말씀 : 일부 OEM의 표준 안드로이드 관행을 따르지 않을 수 있습니다 자신의 연락처 제공 업체 (하지 표준 안드로이드 하나)과 을 제공합니다. 문제가 해결되지 않으면, 당신은 OEM 연락처를 수정해야 할 수 있습니다

// method to get name, contact id, and birthday 
private Cursor getContactsBirthdays() { 
    Uri uri = ContactsContract.Data.CONTENT_URI; 

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

    String where = 
      ContactsContract.Data.MIMETYPE + "= ? AND " + 
      ContactsContract.CommonDataKinds.Event.TYPE + "=" + 
      ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY; 
    String[] selectionArgs = new String[] { 
     ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE 
    }; 
    String sortOrder = null; 
    return managedQuery(uri, projection, where, selectionArgs, sortOrder); 
} 

// iterate through all Contact's Birthdays and print in log 
Cursor cursor = getContactsBirthdays(); 
int bDayColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE); 
while (cursor.moveToNext()) { 
    String bDay = cursor.getString(bDayColumn); 
    Log.d(TAG, "Birthday: " + bDay); 
} 

예를 들어, com.android.providers.contacts.HtcContactsProvider2 내 HTC 디자 이어 HD 여기

에 대한 쿼리에 응답 한 방법이다 공급자.

+0

고마워요! 당신은 또한이 질문을 링크 할 수 있습니다;) http://stackoverflow.com/questions/7376980/android-2-2-contact-birthday-date – caw

1
ContentResolver cr = getContentResolver(); 
String where = Data.raw_contacts_id + " = your_id and " + Data.MIMETYPE + " = " + CommonDataKinds.Events.CONTENT_ITEM_TYPE; 
cr.query(ContactsContract.Data.CONTENT_URI, null, where, null, null); 

내 컴퓨터에 sdk를 설치하지 않았으므로 코드를 테스트하지 않았습니다. 그러나 나는 그것이 효과가 있다고 믿습니다.
희망 사항은 일부 진전에서 도움이되기를 바랍니다.

+1

고마워요! 귀하의 발췌 문장을 내 질문에 코드를 추가했습니다. 이게 당신이 그런 의미일까요? – caw

+0

내 스 니펫을 사용해 보셨습니까? 나는 그것이 효과가 있어야한다고 생각했다. –

+0

예, 사용해 보았습니다. 불행히도 아직 작동하지 않습니다. 그것은 연락처의 외부 루프를 통과하지만 생일의 내부 루프 (String birthday = bdc.getString (0))로 한 번만 이동합니다. 그리고 내부 루프에 들어가는 연락처는 생일이 없습니다. – caw

2

하나는 다음 코드를 사용하여 모든 사용자의 생일을 읽을 수 있습니다

ContentResolver cr = getContentResolver(); 
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
if (cur.getCount() > 0) { 
    while (cur.moveToNext()) { 
     String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); 
     String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
     ContentResolver bd = getContentResolver(); 
     Cursor bdc = bd.query(android.provider.ContactsContract.Data.CONTENT_URI, new String[] { Event.DATA }, android.provider.ContactsContract.Data.CONTACT_ID+" = "+id+" AND "+Data.MIMETYPE+" = '"+Event.CONTENT_ITEM_TYPE+"' AND "+Event.TYPE+" = "+Event.TYPE_BIRTHDAY, null, android.provider.ContactsContract.Data.DISPLAY_NAME); 
     if (bdc.getCount() > 0) { 
      while (bdc.moveToNext()) { 
       String birthday = bdc.getString(0); 
       // now "id" is the user's unique ID, "name" is his full name and "birthday" is the date and time of his birth 
      } 
     } 
    } 
} 
cur.close(); 

을하지만 그것을 할 수있는 더 나은 이하 방법은 무엇입니까? 주의

관련 문제