2011-12-20 2 views
10

Nella mia applicazione Android, ho letto tutti i contatti con il seguente codice:Get compleanno per ogni contatto in applicazione Android

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(); 
     String where = Data.RAW_CONTACT_ID+" = "+id+" and "+Data.MIMETYPE+" = "+CommonDataKinds.Event.CONTENT_ITEM_TYPE; 
     Cursor bdc = bd.query(ContactsContract.Data.CONTENT_URI, null, where, null, null); 
     if (bdc.getCount() > 0) { 
      while (bdc.moveToNext()) { 
       String birthday = bdc.getString(0); 
       Toast.makeText(getApplicationContext(), id+name+birthday, Toast.LENGTH_SHORT); 
      } 
     } 
    } 
} 
cur.close(); 

Ecco come ho provato a leggere l'evento di compleanno per ogni singolo contatto. Ma ovviamente non funziona ancora. Quindi, come posso leggere correttamente la data di nascita del contatto?

risposta

22

Parola di cautela: alcuni OEM di fornire il proprio fornitore di contatto (non quella standard di Android) e possono non seguire le pratiche standard di Android. Ad esempio, com.android.providers.contacts.HtcContactsProvider2 risponde alle domande sul mio HTC Desire HD

Ecco un modo:

// 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); 
} 

Se questo non funziona, si può avere un OEM contatti modificata fornitore.

+0

Grazie! Potresti anche aver collegato questa domanda;) 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); 

Non ho testato il codice poiché non ho installato sdk nel mio computer. Ma credo che dovrebbe funzionare.
Spero che ti possa aiutare in alcuni aspetti.

+1

Grazie! Ho aggiunto il tuo snippet al codice nella mia domanda. È così che volevi che fosse? – caw

+0

Hai provato il mio snippet? Ho pensato che dovrebbe funzionare. –

+0

Sì, l'ho provato. E sfortunatamente, non funziona (ancora). Passa attraverso il loop esterno dei contatti ma solo una volta nel ciclo interno dei compleanni (String birthday = bdc.getString (0)). E il contatto per chi entra nel ciclo interno non ha set di compleanno. – caw

2

Si può leggere tutti compleanno degli utenti con il seguente codice:

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(); 

Ma esiste un modo migliore o più breve per farlo?