Possible Duplicate:
How to read contacts on Android 2.0
I am developing an android application in which i have to implement contacts in 开发者_StackOverflow社区a list and save all the contacts in the database.I have looked around a sample of codes.i have used content provider and cursor adapter for fetching contacts.But it did not worked for me
I had to do a similar thing for my asignment at school. Here is what i ended up with:
private List<String> emailAdresses;
private List<String> readEmailAdresses() {
if(emailAdresses == null)
emailAdresses = new ArrayList<String>();
try {
String emailAdress = null;
Cursor emailCursor = managedQuery(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
new String[] { Email.CONTACT_ID, Email.DATA1 }, null, null,
Email.CONTACT_ID);
while (emailCursor.moveToNext()) {
emailAdress = emailCursor.getString(emailCursor
.getColumnIndex(Email.DATA1));
emailAdresses.add(emailAdress);
}
} catch (Exception ex) {
Log.e(TAG, "Retreive failed", ex);
}
return emailAdresses;
}
Just make sure you use this permission for the example:
<uses-permission android:name="android.permission.READ_CONTACTS" />
精彩评论